一、下載所需jar包
下載地址:http://poi.apache.org/download.html
http://download.csdn.net/detail/likai22/534250
二、上代碼
package com.sxdx.excelpoi.action; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import jxl.Cell; import jxl.CellType; import jxl.NumberCell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.util.CellRangeAddress; /** * HSSF - 提供讀寫Microsoft Excel XLS格式檔案的功能。 XSSF - 提供讀寫Microsoft Excel OOXML XLSX格式檔案的功能。 HWPF - 提供讀寫Microsoft Word DOC97格式檔案的功能。 XWPF - 提供讀寫Microsoft Word DOC2003格式檔案的功能。 HSLF - 提供讀寫Microsoft PowerPoint格式檔案的功能。 HDGF - 提供讀Microsoft Visio格式檔案的功能。 HPBF - 提供讀Microsoft Publisher格式檔案的功能。 HSMF - 提供讀Microsoft Outlook格式檔案的功能。 * */ public class PoiAction { /** * 生成excel * @param args */ public static void main(String[] args) { HSSFWorkbook wb = new HSSFWorkbook();// 創建HSSFWorkbook對象 HSSFSheet sheet = wb.createSheet("sheet0");// 創建HSSFSheet對象 //合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列 sheet.addMergedRegion(new CellRangeAddress(0,0,0,10)); sheet.setDefaultRowHeightInPoints(20);//設置缺省列高 sheet.setDefaultColumnWidth(8);//設置缺省列寬 //設置指定列的列寬,256 * 50這種寫法是因為width參數單位是單個字符的256分之一 sheet.setColumnWidth(0, 256 * 30); // 設置單元格的橫向和縱向對齊方式 HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setAlignment(HorizontalAlignment.CENTER); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //----------------------------------------------------------------------- HSSFRow row0 = sheet.createRow(0);// 創建HSSFRow對象 row0.setHeight((short) 600);//設置行高 HSSFCell cell0 = row0.createCell(0); cell0.setCellValue("考勤結果表"); cell0.setCellStyle(cellStyle); HSSFRow row1 = sheet.createRow(1);// 創建HSSFRow對象 // 創建HSSFCell對象 HSSFCell cell = row.createCell(0) // 設置單元格的值 for(int i=0;i<31;i++){ HSSFCell cell1 = row1.createCell(i); cell1.setCellValue(i+1); cell1.setCellStyle(cellStyle); } HSSFRow row2 = sheet.createRow(2); for(int i=0;i<31;i++){ HSSFCell cell2 = row2.createCell(i); cell2.setCellValue("正常"); cell2.setCellStyle(cellStyle); } HSSFRow row3 = sheet.createRow(3); for(int i=0;i<31;i++){ HSSFCell cell3 = row3.createCell(i); cell3.setCellValue("遲到"); cell3.setCellStyle(cellStyle); } HSSFRow row4 = sheet.createRow(4); for(int i=0;i<31;i++){ HSSFCell cell4 = row4.createCell(i); cell4.setCellValue("請假"); cell4.setCellStyle(cellStyle); } try { // 輸出Excel文件 FileOutputStream output = new FileOutputStream("d:\\workbook.xls"); wb.write(output); output.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 讀取excel */ public static void readExcel(){ //導入已存在的Excel文件,獲得只讀的工作薄對象 FileInputStream fis = null; try { fis = new FileInputStream("d:\\workbook.xls"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Workbook wk = null; try { wk = Workbook.getWorkbook(fis); } catch (BiffException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //獲取第一張Sheet表 Sheet sheet = (Sheet) wk.getSheet(0); //獲取總行數 int rowNum = sheet.getRows(); //從數據行開始迭代每一行 for(int i=0;i<rowNum;i++){ System.out.println(sheet.getCell(0, i).getContents()); } try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } wk.close(); } }
三、main()方法為生成excel, readExcel()為讀取excel。效果圖如下
1、生成文件
2、excel內容
3、讀取excel