import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
HSSFCellStyle類代表一種單元格樣式。可以通過這些類來設置單元格的邊框樣式、背景顏色、字體、水平和垂直方式的對齊。
如:
HSSFWorkbook workbook = new HSSFWorkbook(); //建立一個工作薄
HSSFCellStyle titleStyle=workbook.createCellStyle(); //在工作薄的基礎上建立一個樣式
titleStyle.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE); //設置邊框樣式
titleStyle.setBorderLeft((short)1); //左邊框
titleStyle.setBorderRight((short)1); //右邊框
titleStyle.setBorderTop(HSSFCellStyle.BORDER_DOUBLE); //頂邊框
titleStyle.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index); //填充的背景顏色
titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); //填充圖案
假設什么定義了一個樣式,想在填充第一個單元格的時候填充紅,第二格單元格填充藍色。
如果:
HSSFCellStyle cellStyle = workbook.createCellStyle(); //創建一個樣式
cellStyle.setFillForegroundColor(HSSFColor.RED.index); //設置顏色為紅色
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFCell cell1 = row.createCell((short)1); //給單元格cell1填充紅色
cell1.setCellStyle(cellStyle);
若: cellStyle.setFillForegroundColor(HSSFColor.BLUE.index); //設置顏色為藍色
HSSFCell cell2 = row.createCell((short)2); //給單元格cell2填充藍色
cell2.setCellStyle(cellStyle);
這個時候會出現的現象是單元格cell1和cell2都變成了藍色。遇到這種情況,要預先定義兩種不同的單元格樣式。
當一個EXCEL文件同時需要很多大同小異的單元格樣式時,這樣一一定義很麻煩。POI HSSF提供了一個HSSFCellUtil類(在org.apache.poi. hssf.usermodel.contrib包),里面有幾個方法可以繞過HSSFCellStyle直接設定單元格的樣式,但這幾個方法會拋出NestableException異 常,要處理這個異常,需要引用Apache的幾個Common包:
commons-beanutils.jar
commons-beanutils-bean-collections.jar
commons-beanutils-core.jar
commons-lang.jar
commons-logging-api.jar
以下是其他各種情況的處理:
1、中文處理:
要在通過POI生成的EXCEL中正常顯示中文,需要為單元格設置編碼:
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("部門");
2、合并單元格:
HSSFSheet.addMergedRegion(new Region())方法可以合并單元格,Region()中的一個構造函數含有四個參數,分別代表起始行、起始列、結束 行、結束列:
sheet.addMergedRegion(new Region(initRow, (short)(initCol-2), initRow + lists.size() - 1, (short)(initCol-2)));
3、公式的處理:
HSSFCell.setCellFormula()方法用來在EXCEL單元格中寫入公式。
cell = row.createCell((short)(dataFlag));
cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
cell.setCellFormula("SUM(" + getColLetter(initCol) + (listFlag+1) +":" + getColLetter(dataFlag-1) + (listFlag+1) + ")");
cell.setCellStyle(nameStyle);
4、鏈接的處理:
在POI中往單元格中寫鏈接,是用HYPERLINK函數搞定的。
HYPERLINK函數包含兩個參數,第一個參數是指向的URL地址,第二個參數是顯示字串。
cell = row.createCell((short)(dataFlag));
cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
cell.setCellFormula("HYPERLINK(\"http://www.xxxxx.com/xxx.jsp?id=1\",\"homepage\")");
cell.setCellStyle(linkStyle);
為了使鏈接效果更好,我們可以給鏈接所在單元格定義一種樣式,使鏈接顯示為有下劃線的藍色字串:
HSSFCellStyle linkStyle = workbook.createCellStyle();
linkStyle.setBorderBottom((short)1);
linkStyle.setBorderLeft((short)1);
linkStyle.setBorderRight((short)1);
linkStyle.setBorderTop((short)1);
linkStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
linkStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = workbook.createFont();
font.setFontName(HSSFFont.FONT_ARIAL);
font.setUnderline((byte)1);
font.setColor(HSSFColor.BLUE.index);
linkStyle.setFont(font);
關于代碼對應的顏色如下:
_學而時習之,不亦悅乎.files/8f196ffbe882b9156d22eb96.jpg)
posted on 2007-04-25 19:18
???MengChuChen 閱讀(2056)
評論(0) 編輯 收藏 所屬分類:
java_code