POI编程:
使用java解析和创建office文档的工具.
常用于处理excel文件.
导入jar包
2003版
poi-3.10-FINAL-20140208.jar 核心jar包
lib/*.jar 依赖的jar包.
2007版 建立在2003版基础之上的
poi-3.10-FINAL-20140208.jar 核心jar包
poi-excelant-3.10-FINAL-20140208.jar 文件读写的jar
poi-ooxml-3.10-FINAL-20140208.jar 2007版扩展jar包.
poi-ooxml-schemas-3.10-FINAL-20140208.jar 2007版XML标准jar包.
poi-scratchpad-3.10-FINAL-20140208.jar 做文档格式处理.
lib/*.jar 依赖的jar包.
ooxml-lib/*.jar poi-ooxml jar包依赖的插件
dom4j-1.6.1.jar
stax-api-1.0.1.jar
xmlbeans-2.3.0.jar
POI中行号和列号的计数方式:
行号:
从0开始计数.
lastRowNum是最后一行的行号.
列号:
从0开始计数.
lastCellNum是最后一列编号+1.
1. 解析
1.1 IO流定位文件
1.2 创建POI技术中的excel文档对象工作薄
1.3 解析工作文件Sheet
1.4 解析行
1.5 解析行中的每列
1.6 回收资源
2. 创建
2.1 创建一个空的文档对象.
2.2 在文档中创建Sheet文件
2.3 在文件中创建行
2.4 在行中创建列
2.5 输出文件
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 package test.poi; 2 3 import java.io.FileOutputStream; 4 import java.io.OutputStream; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 9 import org.apache.poi.hssf.util.HSSFColor; 10 import org.apache.poi.ss.usermodel.Cell; 11 import org.apache.poi.ss.usermodel.CellStyle; 12 import org.apache.poi.ss.usermodel.Color; 13 import org.apache.poi.ss.usermodel.Font; 14 import org.apache.poi.ss.usermodel.Row; 15 import org.apache.poi.ss.usermodel.Sheet; 16 import org.apache.poi.ss.usermodel.Workbook; 17 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 18 19 /** 20 * 创建Excel文件 21 */ 22 public class ExportExcel { 23 24 /** 25 * 导出Excel文件 26 * @param List<String[]> 要导出的文件内容. 27 * @param type 导出的文件的格式. 28 */ 29 public void exportExcel(List<String[]> datas, String type) throws Exception{ 30 31 // 1. 创建Excel文档对象 32 Workbook workbook = null; 33 if(type.equals("xls")){ 34 // 2003 35 workbook = new HSSFWorkbook(); 36 }else if(type.equals("xlsx")){ 37 // 2007 38 workbook = new XSSFWorkbook(); 39 }else{ 40 return; 41 } 42 43 // 2. 创建Sheet文件 44 Sheet sheet = workbook.createSheet(); 45 46 // 设置行宽 47 sheet.setColumnWidth(0, 3000); 48 sheet.setColumnWidth(1, 3000); 49 sheet.setColumnWidth(2, 3000); 50 sheet.setColumnWidth(3, 3000); 51 sheet.setColumnWidth(4, 3000); 52 53 // 3. 创建行 54 // 3.1 创建表头 55 // 设置表头内容样式 56 CellStyle headerStyle = workbook.createCellStyle(); 57 headerStyle.setAlignment(CellStyle.ALIGN_CENTER); 58 headerStyle.setBorderBottom(CellStyle.BORDER_THIN); 59 headerStyle.setBorderLeft(CellStyle.BORDER_THIN); 60 headerStyle.setBorderRight(CellStyle.BORDER_THIN); 61 headerStyle.setBorderTop(CellStyle.BORDER_THIN); 62 headerStyle.setFillBackgroundColor(HSSFColor.BLUE.index); 63 Font headerFont = workbook.createFont(); 64 headerFont.setFontName("微软雅黑"); 65 headerFont.setBoldweight((short) 16); 66 headerFont.setColor(HSSFColor.RED.index); 67 headerStyle.setFont(headerFont); 68 69 Row row = sheet.createRow(0); 70 // 4. 创建列 71 Cell cell = row.createCell(0); 72 cell.setCellStyle(headerStyle); 73 cell.setCellValue("卡号"); 74 75 cell = row.createCell(1); 76 cell.setCellStyle(headerStyle); 77 cell.setCellValue("姓名"); 78 79 cell = row.createCell(2); 80 cell.setCellStyle(headerStyle); 81 cell.setCellValue("金额"); 82 83 cell = row.createCell(3); 84 cell.setCellStyle(headerStyle); 85 cell.setCellValue("代扣"); 86 87 cell = row.createCell(4); 88 cell.setCellStyle(headerStyle); 89 cell.setCellValue("备注"); 90 91 for(int i = 0; i < datas.size(); i++){ 92 row = sheet.createRow(i+1); 93 int cellNums = datas.get(i).length; 94 for(int j = 0; j < cellNums; j++){ 95 String cellValue = (datas.get(i))[j]; 96 cell = row.createCell(j); 97 cell.setCellValue(cellValue); 98 } 99 } 100 101 // 5. 输出文件内容 102 // 5.1 创建输出流. 103 OutputStream out = new FileOutputStream("test."+type); 104 105 // 5.2 依托输出流,输出创建的Excel文件内容. 106 workbook.write(out); 107 108 } 109 110 public static void main(String[] args) throws Exception { 111 112 List<String[]> datas = new ArrayList<>(); 113 datas.add(new String[]{"6225880111009999000", "张三", "10000", "500", "工资"}); 114 datas.add(new String[]{"6225880111009999010", "李四", "10000", "500", "工资"}); 115 datas.add(new String[]{"6225880111009999020", "王五", "10000", "500", "工资"}); 116 datas.add(new String[]{"6225880111009999030", "赵六", "10000", "500", "工资"}); 117 118 new ExportExcel().exportExcel(datas, "xls"); 119 120 } 121 122 }
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 package test.poi; 2 3 import java.io.FileInputStream; 4 import java.io.InputStream; 5 import java.text.DecimalFormat; 6 import java.text.SimpleDateFormat; 7 import java.util.ArrayList; 8 import java.util.Arrays; 9 import java.util.Date; 10 import java.util.List; 11 12 import org.apache.poi.hssf.usermodel.HSSFDateUtil; 13 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 14 import org.apache.poi.ss.usermodel.Cell; 15 import org.apache.poi.ss.usermodel.Row; 16 import org.apache.poi.ss.usermodel.Sheet; 17 import org.apache.poi.ss.usermodel.Workbook; 18 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 19 20 /** 21 * 解析Excel文件 22 */ 23 public class ParseExcel { 24 25 /** 26 * 解析Excel文件的方法. 27 * @return 28 */ 29 public List<String[]> parseExcel(String fileName, int startRow) throws Exception{ 30 31 // 1. 输入流 32 InputStream in = new FileInputStream(fileName); 33 34 // 2. 创建Excel文档对象 35 Workbook workbook = null; 36 if(fileName.endsWith(".xls")){ 37 // 2003 38 workbook = new HSSFWorkbook(in); 39 }else if(fileName.endsWith(".xlsx")){ 40 // 2007 41 workbook = new XSSFWorkbook(in); 42 }else{ 43 return null; 44 } 45 46 // 3. 获取Sheet 47 // 根据Sheet文件名获取文件 48 // workbook.getSheet("sheet1"); 49 // 根据下标获取文件 50 Sheet sheet = workbook.getSheetAt(0); 51 52 // 4. 迭代行 53 // 4.1 获取文件中的最后一行.判断开始行是否小于等于最后一行的行号 54 int lastRow = sheet.getLastRowNum(); 55 List<String[]> result = new ArrayList<>(); 56 if(lastRow >= startRow){ 57 58 // 迭代 59 for(int rowNum = startRow; rowNum <= lastRow; rowNum++){ 60 // 根据行号获取行数据 61 Row row = sheet.getRow(rowNum); 62 63 // 5. 迭代行中的单元格. [列] 64 // 5.1 获取首列和尾列的列号 65 int firstCellNum = row.getFirstCellNum(); 66 int lastCellNum = row.getLastCellNum(); 67 String[] rowValues = new String[lastCellNum - firstCellNum]; 68 for(int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++){ 69 String rowValue = ""; 70 // 获取单元格 71 Cell cell = row.getCell(cellNum); 72 // 处理单元格数据 73 if(cell.getCellType() == cell.CELL_TYPE_NUMERIC){ 74 // 数学类型数据, 包含数字,日期 75 if(HSSFDateUtil.isCellDateFormatted(cell)){ 76 // 判断单元格数据是否为日期 77 Date cellValue = cell.getDateCellValue(); 78 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 79 rowValue = sdf.format(cellValue); 80 }else{ 81 // 数学类型 82 double cellValue = cell.getNumericCellValue(); 83 // 创建数学格式化对象 84 DecimalFormat formatter = new DecimalFormat(); 85 formatter.applyPattern("#"); 86 rowValue = formatter.format(cellValue); 87 } 88 }else if(cell.getCellType() == cell.CELL_TYPE_STRING){ 89 // 字符串类型数据 90 rowValue = cell.getRichStringCellValue().getString(); 91 }else if(cell.getCellType() == cell.CELL_TYPE_BLANK){ 92 // 空数据 93 rowValue = ""; 94 } 95 rowValues[cellNum] = rowValue; 96 } 97 result.add(rowValues); 98 } 99 }else{ 100 // 逻辑错误 101 return null; 102 } 103 104 // 6. 回收资源 105 106 return result; 107 } 108 109 public static void main(String[] args) throws Exception { 110 List<String[]> result = new ParseExcel().parseExcel("测试2007.xlsx", 2); 111 for(String[] row : result){ 112 System.out.println(Arrays.toString(row)); 113 } 114 } 115 116 }
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------