需要預先新建編輯好一個excel文件,設定好樣式。
編輯好輸出的數據,依照excel座標一一對應。
支援清單資料輸出,清單中列合併。
程式碼如下:
package com.icourt.util;import org.apache.commons.collections4.CollectionUtils;import org.apache.poi.openxml4j.exceptions.InvalidFormatException;import org.apache.poi.ss.usermodel.*;import org.apache.poi.ss.util.CellRangeAddress;import java.io.*;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * 描述:poi根据模板导出excel,根据excel坐标赋值,如(B1) */public class ExcelExportUtil {//模板mapprivate Map<string> tempWorkbook = new HashMap<string>();//模板输入流mapprivate Map<string> tempStream = new HashMap<string>();/** * 功能:按模板向Excel中相应地方填充数据 */public void writeData(String templateFilePath, Map<string> dataMap, int sheetNo) throws IOException, InvalidFormatException {if (dataMap == null || dataMap.isEmpty()) {return; }//读取模板Workbook wbModule = getTempWorkbook(templateFilePath);//数据填充的sheetSheet wsheet = wbModule.getSheetAt(sheetNo);for (Entry<string> entry : dataMap.entrySet()) { String point = entry.getKey(); Object data = entry.getValue(); TempCell cell = getCell(point, data, wsheet);//指定坐标赋值 setCell(cell, wsheet); }//设置生成excel中公式自动计算wsheet.setForceFormulaRecalculation(true); }/** * 功能:按模板向Excel中列表填充数据.只支持列合并 */public void writeDateList(String templateFilePath, String[] heads, List<map>> datalist, int sheetNo) throws IOException, InvalidFormatException {if (heads == null || heads.length tempCells = new ArrayList<tempcell>(heads.length);for (String point : heads) { TempCell tempCell = getCell(point, null, wsheet);//取得合并单元格位置 -1:表示不是合并单元格int pos = isMergedRegion(wsheet, tempCell.getRow(), tempCell.getColumn());if (pos > -1) { CellRangeAddress range = wsheet.getMergedRegion(pos); tempCell.setColumnSize(range.getLastColumn() - range.getFirstColumn()); } tempCells.add(tempCell); }//赋值for (int i = 0; i dataMap = datalist.get(i);for (int j = 0; j -1) { CellRangeAddress rangeAddress = mergeRegion(sheet, tempCell.getRow(), tempCell.getRow(), tempCell.getColumn(), tempCell.getColumn() + tempCell.getColumnSize()); setRegionStyle(tempCell.getCellStyle(), rangeAddress, sheet); } Row rowIn = sheet.getRow(tempCell.getRow());if (rowIn == null) { copyRows(tempCell.getRow() - 1, tempCell.getRow() - 1, tempCell.getRow(), sheet);//复制上一行rowIn = sheet.getRow(tempCell.getRow()); } Cell cellIn = rowIn.getCell(tempCell.getColumn());if (cellIn == null) { cellIn = rowIn.createCell(tempCell.getColumn()); }//根据data类型给cell赋值if (tempCell.getData() instanceof String) { cellIn.setCellValue((String) tempCell.getData()); } else if (tempCell.getData() instanceof Integer) { cellIn.setCellValue((int) tempCell.getData()); } else if (tempCell.getData() instanceof Double) { cellIn.setCellValue((double) tempCell.getData()); } else { cellIn.setCellValue((String) tempCell.getData()); }//样式if (tempCell.getCellStyle() != null && tempCell.getColumnSize() == -1) { cellIn.setCellStyle(tempCell.getCellStyle()); } }/** * 功能:写到输出流并移除资源 */public void writeAndClose(String templateFilePath, OutputStream os) throws IOException, InvalidFormatException {if (getTempWorkbook(templateFilePath) != null) { getTempWorkbook(templateFilePath).write(os); tempWorkbook.remove(templateFilePath); }if (getInputStream(templateFilePath) != null) { getInputStream(templateFilePath).close(); tempStream.remove(templateFilePath); } }/** * 功能:判断指定的单元格是否是合并单元格 */private Integer isMergedRegion(Sheet sheet, int row, int column) {for (int i = 0; i = firstRow && row = firstColumn && column = pStartRow)&& (region.getLastRow() dataMap = new HashMap<string>(); dataMap.put("B1", "03_Alpha_项目工作时间统计表"); dataMap.put("B2", "统计时间:2017/01/01 - 2017/03/31"); excel.writeData(templateFilePath, dataMap, 0); List<map>> datalist = new ArrayList<map>>(); Map<integer> data = new HashMap<integer>(); data.put(1, "3/10/17"); data.put(2, "18:50"); data.put(3, "19:00"); data.put(4, "李子鹏"); data.put(5, "新增项目键值对接口,供任务计时调用"); data.put(6, "代码开发"); data.put(7, "3.17"); datalist.add(data); data = new HashMap<integer>(); data.put(1, "3/10/17"); data.put(2, "18:50"); data.put(3, "19:00"); data.put(4, "李子鹏"); data.put(5, "新增项目键值对接口,供任务计时调用"); data.put(6, "代码开发"); data.put(7, "3.17"); datalist.add(data); data = new HashMap<integer>(); data.put(1, "3/10/17"); data.put(2, "18:50"); data.put(3, "19:00"); data.put(4, "李子鹏"); data.put(5, "新增项目键值对接口,供任务计时调用"); data.put(6, "代码开发"); data.put(7, "3.17"); datalist.add(data); data = new HashMap<integer>(); data.put(1, "3/10/17"); data.put(2, "18:50"); data.put(3, "19:00"); data.put(4, "李子鹏"); data.put(5, "新增项目键值对接口,供任务计时调用"); data.put(6, "代码开发"); data.put(7, "3.17"); datalist.add(data); data = new HashMap<integer>(); data.put(1, "3/10/17"); data.put(2, "18:50"); data.put(3, "19:00"); data.put(4, "李子鹏"); data.put(5, "新增项目键值对接口,供任务计时调用"); data.put(6, "代码开发"); data.put(7, "3.17"); datalist.add(data); data = new HashMap<integer>(); data.put(1, "3/10/17"); data.put(2, "18:50"); data.put(3, "19:00"); data.put(4, "李子鹏"); data.put(5, "新增项目键值对接口,供任务计时调用"); data.put(6, "代码开发"); data.put(7, "3.17"); datalist.add(data); data = new HashMap<integer>(); data.put(1, "3/10/17"); data.put(2, "18:50"); data.put(3, "19:00"); data.put(4, "李子鹏"); data.put(5, "新增项目键值对接口,供任务计时调用"); data.put(6, "代码开发"); data.put(7, "3.17"); datalist.add(data); data = new HashMap<integer>(); data.put(1, "3/10/17"); data.put(2, "18:50"); data.put(3, "19:00"); data.put(4, "李子鹏"); data.put(5, "新增项目键值对接口,供任务计时调用"); data.put(6, "代码开发"); data.put(7, "3.17"); datalist.add(data); data = new HashMap<integer>(); data.put(1, "3/10/17"); data.put(2, "18:50"); data.put(3, "19:00"); data.put(4, "李子鹏"); data.put(5, "新增项目键值对接口,供任务计时调用新增项目键值对接口,供任务计时调用新增项目键值对接口,供任务计时调用新增项目键值对接口,供任务计时调用新增项目键值对接口,供任务计时调用新增项目键值对接口,供任务计时调用新增项目键值对接口,供任务计时调用新增项目键值对接口,供任务计时调用"); data.put(6, "代码开发"); data.put(7, "3.17"); datalist.add(data); data = new HashMap<integer>(); data.put(1, ""); data.put(2, ""); data.put(3, ""); data.put(4, ""); data.put(5, ""); data.put(6, ""); data.put(7, ""); datalist.add(data); String[] heads = new String[]{"B4", "C4", "D4", "E4", "F4", "G4", "H4"}; excel.writeDateList(templateFilePath, heads, datalist, 0);//写到输出流并移除资源 excel.writeAndClose(templateFilePath, os); os.flush(); os.close(); } }</integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></integer></map></map></string></tempcell></map></string></string></string></string></string></string>
#
大致思路:
##最主要是製作好模版
#程式碼根據模版,讀取設定好的列的格式,在循環資料行,讀取模版中的對應的行,存在該行就取得,不存在看是否需要copy某一行,不需要就手動創建無制定格式的行,後面在為該行的每一列對應的給個單元格製定格式和資料。
以上是apache poi根據模板匯出excel的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Java在企業級應用中被廣泛使用是因為其平台獨立性。 1)平台獨立性通過Java虛擬機(JVM)實現,使代碼可在任何支持Java的平台上運行。 2)它簡化了跨平台部署和開發流程,提供了更大的靈活性和擴展性。 3)然而,需注意性能差異和第三方庫兼容性,並採用最佳實踐如使用純Java代碼和跨平台測試。

JavaplaysigantroleiniotduetoitsplatFormentence.1)itallowscodeTobewrittenOnCeandrunonVariousDevices.2)Java'secosystemprovidesuseusefidesusefidesulylibrariesforiot.3)

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

Java'splatFormIndenceistificantBecapeitAllowSitallowsDevelostWriTecoDeonCeandRunitonAnyPlatFormwithAjvm.this“ writeonce,runanywhere”(era)櫥櫃櫥櫃:1)交叉plat formcomplibility cross-platformcombiblesible,enablingDeploymentMentMentMentMentAcrAptAprospOspOspOssCrossDifferentoSswithOssuse; 2)

Java適合開發跨服務器web應用。 1)Java的“一次編寫,到處運行”哲學使其代碼可在任何支持JVM的平台上運行。 2)Java擁有豐富的生態系統,包括Spring和Hibernate等工具,簡化開發過程。 3)Java在性能和安全性方面表現出色,提供高效的內存管理和強大的安全保障。

JVM通過字節碼解釋、平台無關的API和動態類加載實現Java的WORA特性:1.字節碼被解釋為機器碼,確保跨平台運行;2.標準API抽像操作系統差異;3.類在運行時動態加載,保證一致性。

Java的最新版本通過JVM優化、標準庫改進和第三方庫支持有效解決平台特定問題。 1)JVM優化,如Java11的ZGC提升了垃圾回收性能。 2)標準庫改進,如Java9的模塊系統減少平台相關問題。 3)第三方庫提供平台優化版本,如OpenCV。

JVM的字節碼驗證過程包括四個關鍵步驟:1)檢查類文件格式是否符合規範,2)驗證字節碼指令的有效性和正確性,3)進行數據流分析確保類型安全,4)平衡驗證的徹底性與性能。通過這些步驟,JVM確保只有安全、正確的字節碼被執行,從而保護程序的完整性和安全性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器