[poi] poi操作Excel
package com.wa.poi.excel.xls; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; /** * @Date 2015-02-12 * poi解析xls文件 */ public class XlsParse { public static void main(String[] args) throws Exception { XlsParse parse = new XlsParse(); List<XlsDojo> list = parse.readXls(); parse.xlsDojo2Excel(list); for (XlsDojo dojo:list){ System.out.println(dojo.getNO()+" "+dojo.getName() +" "+dojo.getSubject()+" "+dojo.getScore()+" " +dojo.getPass()); } System.out.println("\n"); } /** * 读取xls文件内容 * @return */ public List<XlsDojo> readXls() { InputStream inputStream; List<XlsDojo> list=null; try { inputStream = new FileInputStream("D:\\Workspaces\\MyEclipse 8.5\\poi\\excel\\score.xls"); //工作表 HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream); XlsDojo xlsDojo = null; list = new ArrayList<XlsDojo>(); // 循环工作表Sheet for (int i=0;i<hssfWorkbook.getNumberOfSheets();i++){ HSSFSheet sheet =hssfWorkbook.getSheetAt(i); if (sheet == null) { continue; } // 循环行Row for (int rowNum = 1;rowNum <= sheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow =sheet.getRow(rowNum); if (hssfRow == null) { continue; } xlsDojo= new XlsDojo(); HSSFCell no = hssfRow.getCell(0); if (no==null){ continue; } xlsDojo.setNO(getCellValue(no)); HSSFCell name = hssfRow.getCell(1); if (name==null){ continue; } xlsDojo.setName(getCellValue(name)); HSSFCell subject = hssfRow.getCell(2); if (subject==null){ continue; } xlsDojo.setSubject(getCellValue(subject)); HSSFCell score = hssfRow.getCell(3); if (score==null){ continue; } xlsDojo.setScore(getCellValue(score)); HSSFCell pass = hssfRow.getCell(4); if (pass==null){ continue; } xlsDojo.setPass(getCellValue(pass)); list.add(xlsDojo); } } } catch (Exception e) { e.printStackTrace(); } return list; } /** * 将数据写入到excel中 * @param xls * @throws Exception */ public void xlsDojo2Excel(List<XlsDojo> xls) throws Exception{ // 获取总列数 int CountColumnNum = xls.size(); // 创建Excel文档 HSSFWorkbook hwb = new HSSFWorkbook(); XlsDojo xlsDto = null; // sheet 对应一个工作页 HSSFSheet sheet = hwb.createSheet("pd"); HSSFRow firstrow = sheet.createRow(0); // 下标为0的行开始 HSSFCell[] firstcell = new HSSFCell[CountColumnNum]; String[] names = new String[CountColumnNum]; names[0] = "学号"; names[1] = "姓名"; names[2] = "学科"; names[3] = "成绩"; names[4] = "是否通过"; for (int j = 0; j < CountColumnNum; j++) { firstcell[j] = firstrow.createCell(j); firstcell[j].setCellValue(new HSSFRichTextString(names[j])); } for (int i = 0; i < xls.size(); i++) { // 创建一行 HSSFRow row = sheet.createRow(i + 1); // 得到要插入的每一条记录 xlsDto = xls.get(i); for (int colu = 0; colu <= 4; colu++) { // 在一行内循环 HSSFCell xh = row.createCell(0); xh.setCellValue(xlsDto.getNO()); HSSFCell xm = row.createCell(1); xm.setCellValue(xlsDto.getName()); HSSFCell yxsmc = row.createCell(2); yxsmc.setCellValue(xlsDto.getSubject()); HSSFCell kcm = row.createCell(3); kcm.setCellValue(xlsDto.getScore()); HSSFCell cj = row.createCell(4); cj.setCellValue(xlsDto.getPass()); } } // 创建文件输出流,准备输出电子表格 OutputStream out = new FileOutputStream("excel/pldrxkxxmb.xls"); hwb.write(out); out.close(); System.out.println("数据库导出成功"); } /** * 判断单元格中的值类型 * @param cell * @return */ private static String getCellValue(HSSFCell cell){ // 返回布尔类型的值 if (cell.getCellType()==HSSFCell.CELL_TYPE_BOOLEAN){ return String.valueOf(cell.getBooleanCellValue()); // 返回数值类型的值 }else if(cell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){ return String.valueOf(cell.getNumericCellValue()); // 返回字符串类型的值 }else { return String.valueOf(cell.getStringCellValue()); } } }
score.xls内容:

同时导出到excel中的内容是:


浙公网安备 33010602011771号