POI 3.17使用筆記
阿新 • • 發佈:2019-01-01
1.新建springboot專案
2.匯入POI相關maven依賴
<!--poi--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.17</version> </dependency>
3.匯出excel(excel的建立,單元格字型樣式,單元格寬高)
package lucene; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import java.io.FileOutputStream; /** * lucene全文檢索測試類 * Created by Administrator on 2018/7/10. */ public class LuceneTestController { //poi匯出excel public void exportExcel() throws Exception{ HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("sheet name"); //合併單元格 sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 5)); //設定第一列單元格寬度 sheet.setColumnWidth(0,100*100); //設定第二列單元格寬度 sheet.setColumnWidth(1,100*100); //建立第一行 HSSFRow row0 = sheet.createRow(0); //建立第二行 HSSFRow row1 = sheet.createRow(1); //設定第一行單元格高度 row0.setHeight((short) 400); //建立第一行第一列單元格 HSSFCell cell0_1 = row0.createCell(0); //建立第二行第一列單元格 HSSFCell cell0_2 = row1.createCell(0); //設定單元格的值 cell0_1.setCellValue("專案施工進度管理系統"); //改變字型樣式,步驟 HSSFFont hssfFont = wb.createFont(); //設定字型,紅色 hssfFont.setColor(HSSFFont.COLOR_RED); //字型粗體顯示 hssfFont.setBold(true); hssfFont.setFontName("宋體"); // 字型大小 hssfFont.setFontHeightInPoints((short) 22); //設定樣式 HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setFont(hssfFont); //設定單元格背景色 cellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex()); cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); //設定居中 cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 //設定邊框 cellStyle.setBorderBottom(BorderStyle.THIN); //下邊框 cellStyle.setBorderLeft(BorderStyle.DASH_DOT_DOT);//左邊框 cellStyle.setBorderTop(BorderStyle.THIN);//上邊框 cellStyle.setBorderRight(BorderStyle.THIN);//右邊框 //3.單元格使用樣式,設定第一行第一列單元格樣式 cell0_1.setCellStyle(cellStyle); cell0_2.setCellStyle(cellStyle); //生成excel檔案 FileOutputStream fileOut = new FileOutputStream("e:\\"+System.currentTimeMillis()+".xls"); wb.write(fileOut); fileOut.close(); /* struts匯出excel,前端需要是提交form形式,否則,點選匯出不會彈出框 HSSFWorkbook wb = exportExcel(projectId, blockId, buildingId,buildingCode); // 生成excel檔案 String fileName = String.valueOf(Calendar.getInstance().getTimeInMillis()).concat(".xls"); // 清空response this.getResponse().reset(); this.getResponse().addHeader("Content-Disposition","attachment;filename=" + new String(fileName.getBytes())); this.getResponse().setContentType("application/vnd.ms-excel;charset=utf-8"); OutputStream os = this.getResponse().getOutputStream(); wb.write(os); if (os != null) { os.close(); os = null; } wb.close(); */ } }