1. 程式人生 > >使用poi通過excel模板匯出excel資料

使用poi通過excel模板匯出excel資料

期望:用預先的xlsx的Excel表格模板

參考:兩篇部落格

步驟:

模板+資料:


相關jar報 座標:(注意jar版本 否則再

tempWorkBook = new XSSFWorkbook(inputstream);

會報  classnotFoundeException!!!!!)\

pom:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.8</version>
</dependency
> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.8</version> </dependency> <dependency> <groupId>org.apache.xmlbeans</groupId> <artifactId>xmlbeans</artifactId>
<version>2.3.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency>

程式碼:

@RequestMapping(value = "/export",method = RequestMethod
.POST) public void ExportExcell(HttpServletRequest request, HttpServletResponse response){ try { ExcellUtil excellUtil=new ExcellUtil(); List<Map<String,String>> data=new ArrayList<>(); for (int i=0; i<2 ;i++) { Map map=new HashMap(); map.put("preAmount","2.0"); map.put("plusAmount","2.0"); map.put("reduceAmount","2.0"); map.put("afterAmount","2.0"); map.put("accountsType","66"); data.add(map); } File file=new File("D:\\test.xlsx"); if (!file.exists()){ file.createNewFile(); } OutputStream outputStream=new FileOutputStream(file); excellUtil.createExcel(data,outputStream,request); } catch (Exception e) { e.printStackTrace(); } }

excelUtils.java

public void createExcel(List<Map<String, String>> list, OutputStream output,HttpServletRequest request) {
Workbook tempWorkBook = null;
    Sheet tempSheet = null;
    int rowIndex = 5;
    Row tempRow = null;
    Cell tempCell = null;
    InputStream inputstream = null;
    try {
//inputstream = request.getSession().getServletContext().getResourceAsStream("/templates/test.xlsx");
        //inputstream=ExcellUtil.class.getResourceAsStream("/templates/test.xlsx");
String path = ExcellExportController.class.getResource("/").getPath() + "/templates/test.xlsx";
        File file=new File(path);
        boolean exists = file.exists();
        System.out.println(exists);
        FileInputStream fileInputStream=new FileInputStream(file);
        inputstream=fileInputStream;
        // 獲取模板
tempWorkBook = new XSSFWorkbook(inputstream);
        // 獲取模板sheettempSheet = tempWorkBook.getSheetAt(0);

        // 用於統計的變數
double preAmount = 0;
        double plusAmount = 0;
        double reduceAmount = 0;
        double afterAmount = 0;

        // 將資料寫入excel
for (int i = 0; i < list.size(); i++) {
int cellNo = 0;
            // 獲取指定行
tempRow = tempSheet.getRow(0);

            //獲取當前sheet最後一行資料對應的行索引
int currentLastRowIndex = tempSheet.getLastRowNum();
            int newRowIndex = currentLastRowIndex + 1;
            XSSFRow newRow = (XSSFRow) tempSheet.createRow(newRowIndex);
            //開始建立並設定該行每一單元格的資訊,該行單元格的索引從 0 開始
int cellIndex = 0;

            //建立一個單元格,設定其內的資料格式為字串,並填充內容,其餘單元格類同
XSSFCell newNameCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_STRING);
            newNameCell.setCellValue(list.get(i).get("accountsType"));
            XSSFCell newGenderCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_STRING);
            newGenderCell.setCellValue(list.get(i).get("preAmount"));
            XSSFCell newAgeCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_NUMERIC);
            newAgeCell.setCellValue(list.get(i).get("curpreAmount"));
            XSSFCell newAddressCell = newRow.createCell(cellIndex++, Cell.CELL_TYPE_NUMERIC);
            newAddressCell.setCellValue(list.get(i).get("reduceAmount"));
}
// 將內容寫入Excel
tempWorkBook.write(output);
    } catch (Exception e) {
e.printStackTrace();
    } finally {
try {
output.close();
        } catch (IOException e) {
e.printStackTrace();
        }
    }
}至此結束。