用POI創建EXCEL表格的幾個例子
POI為應用最廣的,EXCEL報表生成工具,可以在http://www.apache.org/dyn/closer.cgi/jakarta/poi/上得到最新的下載包.
一,生成一個簡單的EXCEL文件;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileOutputStream;
public class CreateXL {
?/** Excel 文件要存放的位置 */
?public static String outputFile = "c:/ excel.xls";
?public static void main(String args[]) {
??try {
???// 創建新的Excel 工作簿
???HSSFWorkbook workbook = new HSSFWorkbook();
???// 在Excel工作簿中建一工作表,其名為缺省值
???// 如要新建一名為"效益指標"的工作表,其語句為:
???HSSFSheet sheet = workbook.createSheet("Sheet1");
???// HSSFSheet sheet = workbook.createSheet();
???// 在索引0的位置創建行(最頂端的行)
???HSSFRow row = sheet.createRow((short) 0);
???// 在索引0的位置創建單元格(左上端)
???HSSFCell cell = row.createCell((short) 0);
???// 定義單元格為字符串類型
???cell.setCellType(HSSFCell.CELL_TYPE_STRING);
???// 在單元格中輸入一些內容
???cell.setCellValue(" number 1");
???HSSFCell cell01 = row.createCell((short)1);
???// 定義單元格為字符串類型
???cell01.setCellType(HSSFCell.CELL_TYPE_STRING);
???// 在單元格中輸入一些內容
???cell01.setCellValue(" number2");???
???
???
???// 新建一輸出文件流
???FileOutputStream fOut = new FileOutputStream(outputFile);
???// 把相應的Excel 工作簿存盤
???workbook.write(fOut);
???fOut.flush();
???// 操作結束,關閉文件
???fOut.close();
???System.out.println("文件生成...");
??} catch (Exception e) {
???System.out.println("已運行 xlCreate() : " + e);
??}
?}
}
二生成一個包含中文的EXCEL文件;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class CreateCells
{
?public static void main(String[] args)
?throws IOException
?{
??HSSFWorkbook wb = new HSSFWorkbook();// 建立新HSSFWorkbook對象
??HSSFSheet sheet = wb.createSheet("new sheet");// 建立新的sheet對象
??// Create a row and put some cells in it. Rows are 0 based.
??HSSFRow row = sheet.createRow((short) 0);// 建立新行
??// Create a cell and put a value in it.
??HSSFCell cell = row.createCell((short) 0);// 建立新cell
??cell.setCellValue(1);// 設置cell的整數類型的值
??// Or do it on one line.
??row.createCell((short) 1).setCellValue(1.2);// 設置cell浮點類型的值
??row.createCell((short) 2).setCellValue("test");// 設置cell字符類型的值
??row.createCell((short) 3).setCellValue(true);// 設置cell布爾類型的值
??HSSFCellStyle cellStyle = wb.createCellStyle();// 建立新的cell樣式
??cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));// 設置cell樣式為定制的日期格式
??HSSFCell dCell = row.createCell((short) 4);
??dCell.setCellValue(new Date());// 設置cell為日期類型的值
??dCell.setCellStyle(cellStyle); // 設置該cell日期的顯示格式
??HSSFCell csCell = row.createCell((short) 5);
??csCell.setEncoding(HSSFCell.ENCODING_UTF_16);// 設置cell編碼解決中文高位字節截斷
??csCell.setCellValue("中文測試_Chinese Words Test");// 設置中西文結合字符串
??row.createCell((short) 6).setCellType(HSSFCell.CELL_TYPE_ERROR);// 建立錯誤cell
??// Write the output to a file
??FileOutputStream fileOut = new FileOutputStream("c:/workbook.xls");
??wb.write(fileOut);
??fileOut.close();
?}
}
三讀取EXCEL文件;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileInputStream;
public class ReadXL {
?/** Excel文件的存放位置。注意是正斜線 */
?public static String fileToBeRead =? "c:/ excel.xls";
?public static void main(String args[]) {
??try {
???// 創建對Excel工作簿文件的引用
???HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
?????fileToBeRead));
???// 創建對工作表的引用。
???// 本例是按名引用(讓我們假定那張表有著缺省名"Sheet1")
???HSSFSheet sheet = workbook.getSheet("Sheet1");
???// 也可用getSheetAt(int index)按索引引用,
???// 在Excel文檔中,第一張工作表的缺省索引是0,
???// 其語句為:HSSFSheet sheet = workbook.getSheetAt(0);
???// 讀取左上端單元
???HSSFRow row = sheet.getRow(0);
???HSSFCell cell = row.getCell((short) 0);
???// 輸出單元內容,cell.getStringCellValue()就是取所在單元的值
???System.out.println("左上端單元是: " + cell.getStringCellValue());
??} catch (Exception e) {
???System.out.println("已運行xlRead() : " + e);
??}
?}
}
以下是一些應用:
1、創建字體,設置其為紅色、粗體:
HSSFFont font = workbook.createFont();
font.setColor(HSSFFont.COLOR_RED);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
2、創建格式
HSSFCellStyle cellStyle= workbook.createCellStyle();
cellStyle.setFont(font);
3、應用格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellStyle(cellStyle);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("標題 ");