<dependencies> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.9</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.9</version> </dependency> </dependencies>
Excel文件apache
|
sheet:工做表,在這個級別對列寬進行控制
|
row:行
|
cell:單元格app
@Test public void testRead() throws Exception { //1.建立Workbook對象 Workbook wb = WorkbookFactory.create(new File("C:\\Users\\Administrator\\Desktop\\1.xlsx")); //2.從Workbook對象中獲取第一個sheet Sheet sheet = wb.getSheetAt(0); //3.從第一個sheet中獲得第一行數據 Row row = sheet.getRow(0); //4.從第一行中獲取第一列數據 Cell cell = row.getCell(0); System.out.println(cell.getCellType()); System.out.println(cell.getStringCellValue()); }
//將不一樣的數據類型,轉換成string類型的! private String getCellValue(Cell c){ String o = null; switch(c.getCellType()){ case Cell.CELL_TYPE_BLANK: o = ""; break; case Cell.CELL_TYPE_BOOLEAN: o = String.valueOf(c.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: o = String.valueOf(c.getCellFormula()); break; case Cell.CELL_TYPE_NUMERIC: o = String.valueOf(c.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: o = c.getStringCellValue(); break; default: o = null; break; } return o; }
測試 ide
@Test public void testForeachExcel() throws Exception{ Workbook wb = WorkbookFactory.create(new File("C:\\Users\\Administrator\\Desktop\\1.xlsx")); Sheet sheet = wb.getSheetAt(0); Row row = null; //獲取每一行 for(int i = 0 ; i< sheet.getLastRowNum();i++){ row = sheet.getRow(i); //對每一行的每一列遍歷! for(int j=0;j<row.getLastCellNum();j++){ System.out.print(getCellValue(row.getCell(j))+"======"); } System.out.println(); } }
變1:固然上面的讀取方式從POI3.8開始也是支持加強for循環讀取數據的!測試
@Test public void testForeachExcel() throws Exception{ Workbook wb = WorkbookFactory.create(new File("C:\\Users\\Administrator\\Desktop\\1.xlsx")); Sheet sheet = wb.getSheetAt(0); //獲取每一行 for(Row row : sheet){ for(Cell cell:row){ System.out.print(getCellValue(cell)+"---"); } System.out.println(); } }
@Test public void testWriteExcel() throws Exception{ Workbook wb = new HSSFWorkbook(); FileOutputStream fos = new FileOutputStream("D:/1.xlsx"); wb.write(fos); if(fos != null){ fos.close(); } }
1)建立表明一個Excel文件的HSSFWorkbook對象spa
HSSFWorkbook workbook = new HSSFWorkbook();
2)建立表明一個工做表的HSSFSheet對象
HSSFSheet sheet = workbook.createSheet("工做表名稱");
3)建立表明行的HSSFRow對象
HSSFRow row = sheet.createRow(index); //index表示行的索引,從0開始
4)建立表明單元格的HSSFCell對象
HSSFCell cell = row.createCell(index); //index表示單元格的索引,從0開始
5)將Excel文件寫入到文件系統中
①.建立一個文件輸出流對象
FileOutputStream outputStream = new FileOutputStream("文件路徑");
②.將文件內容寫入到這個輸出流
workbook.write(outputStream);
案例1:3d
@Test public void test() throws Exception { //1.建立表明Excel文件的HSSFWorkBook對象 HSSFWorkbook workbook = new HSSFWorkbook(); //2.在當前Excel文件中建立工做表 HSSFSheet sheet = workbook.createSheet(); //3.在當前工做表中,建立行 HSSFRow row = sheet.createRow(0); //4.在當前行中,建立單元格 HSSFCell cell = row.createCell(0); //5.給單元格設置值 cell.setCellValue("不要迷戀哥,哥只是個傳說!"); //6.建立輸出流,寫入到硬盤上! FileOutputStream out = new FileOutputStream("d:\\text.xls"); workbook.write(out); }
案例2:excel
@Test public void test() throws Exception { //1.建立表明Excel文件的HSSFWorkBook對象 HSSFWorkbook workbook = new HSSFWorkbook(); //2.在當前Excel文件中建立工做表 HSSFSheet sheet = workbook.createSheet(); //3.在當前工做表中,建立行 HSSFRow row = sheet.createRow(0); //4.在當前行中,建立單元格 HSSFCell cell = row.createCell(0); //5.①.給單元格設置字符串值 cell.setCellValue("不要迷戀哥,哥只是個傳說!"); //②.給單元格設置布爾型值 HSSFCell cell2 = row.createCell(1); cell2.setCellValue(true); //③.給單元格設置整數值 HSSFCell cell3 = row.createCell(2); cell3.setCellValue(10); HSSFCell cell4 = row.createCell(3); cell4.setCellValue(3.4); //④.給單元格設置時間值 HSSFCell cell5 = row.createCell(4); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = sdf.parse("1990-12-12 10:38:40"); cell5.setCellValue(date); //⑤.給單元格設置Calendar時間 HSSFCell cell6 = row.createCell(5); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, 1990); calendar.set(Calendar.MONTH,12); calendar.set(Calendar.DAY_OF_MONTH,20); calendar.set(Calendar.HOUR,10); calendar.set(Calendar.MINUTE,50); calendar.set(Calendar.SECOND,55); cell6.setCellValue(calendar); //6.建立輸出流,寫入到硬盤上! FileOutputStream out = new FileOutputStream("d:\\text.xls"); workbook.write(out); }
爲了不處理工做表數據時內存溢出,相關對象要儘量重用,而不是每次都建立新的。
HSSFDataFormat format = workbook.createDataFormat();
①.日期格式
HSSFCellStyle styleDate = workbook.createCellStyle();
styleDate.setDataFormat(format.getFormat("yyyy/MM/dd HH:dd:ss"));
③.迴繞文本
HSSFCellStyle styleWrapText = workbook.createCellStyle();
styleWrapText.setWrapText(true);code
④.指定列寬:單位1/20像素
sheet.setColumnWidth(columnIndex,width);orm
案例:設置時間的格式xml
@Test public void test() throws Exception { //1.建立表明Excel文件的HSSFWorkBook對象 HSSFWorkbook workbook = new HSSFWorkbook(); //2.在當前Excel文件中建立工做表 HSSFSheet sheet = workbook.createSheet(); //3.在當前工做表中,建立行 HSSFRow row = sheet.createRow(0); HSSFDataFormat dataFormat = workbook.createDataFormat(); short format = dataFormat.getFormat("yyyy-MM-dd HH:mm:ss"); HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(format); //④.給單元格設置時間值 HSSFCell cell5 = row.createCell(4); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = sdf.parse("1990-12-12 10:38:40"); cell5.setCellValue(date); cell5.setCellStyle(cellStyle); //⑤.給單元格設置Calendar時間 HSSFCell cell6 = row.createCell(5); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, 1990); calendar.set(Calendar.MONTH,12); calendar.set(Calendar.DAY_OF_MONTH,20); calendar.set(Calendar.HOUR,10); calendar.set(Calendar.MINUTE,50); calendar.set(Calendar.SECOND,55); cell6.setCellValue(calendar); cell6.setCellStyle(cellStyle); }
案例:設置迴繞文本
@Test public void test() throws Exception { //1.建立表明Excel文件的HSSFWorkBook對象 HSSFWorkbook workbook = new HSSFWorkbook(); //2.在當前Excel文件中建立工做表 HSSFSheet sheet = workbook.createSheet(); //3.在當前工做表中,建立行 HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); cell.setCellValue("這是一個迴繞文本數據哦!!!!!!!!!"); HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setWrapText(true); cell.setCellStyle(cellStyle); //6.建立輸出流,寫入到硬盤上! FileOutputStream out = new FileOutputStream("d:\\text.xls"); workbook.write(out); }
案例:設置固定列寬
@Test public void test() throws Exception { //1.建立表明Excel文件的HSSFWorkBook對象 HSSFWorkbook workbook = new HSSFWorkbook(); //2.在當前Excel文件中建立工做表 HSSFSheet sheet = workbook.createSheet(); //設置固定列寬 sheet.setColumnWidth(1,10000); //3.在當前工做表中,建立行 HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); cell.setCellValue("這是一個迴繞文本數據哦!!!!!!!!!"); HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setWrapText(true); cell.setCellStyle(cellStyle); //6.建立輸出流,寫入到硬盤上! FileOutputStream out = new FileOutputStream("d:\\text.xls"); workbook.write(out); }
commons-codec-1.5.jar
poi-3.9-20121203.jar
①提供文件輸入流:inputstream
②提供文件名:fileName
@Component public class MyView extends AbstractView{ @Override protected void renderMergedOutputModel(Map<String, Object> map, HttpServletRequest request, HttpServletResponse response) throws Exception { HSSFWorkbook wb = new HSSFWorkbook(); HSSFDataFormat format = wb.createDataFormat(); HSSFCellStyle style = wb.createCellStyle(); HSSFSheet sheet = wb.createSheet("sheet001"); HSSFRow row = sheet.createRow(0); style = wb.createCellStyle(); HSSFCell cell = row.createCell(1); System.out.println(map.get("name")); cell.setCellValue((String)(map.get("name"))); cell.setCellStyle(style); sheet.autoSizeColumn(1); response.setContentType("application/vnd.ms-excel"); String fileName = "DFS.xls"; response.setHeader("Content-Disposition","attachment; filename="+fileName); wb.write(response.getOutputStream()); } }