【帶資訊的Excel模板下載】
最近在專案在做Excel下載與上傳,本篇部落格主要來進行總結,講知識分類歸倉。
程式碼:
public static void restExcel(Map<String,List<Map<String,Object>>> data,String fileName){
int tableMarginRow= 0;
String[] title = null;
List<Map<String,Object>> tableData = null;
HSSFWorkbook wb = new HSSFWorkbook();
//資料樣式:
HSSFCellStyle cellDataStyle = wb.createCellStyle();
cellDataStyle = wb.createCellStyle();
org.apache.poi.hssf.usermodel.HSSFFont cellDatafont = wb.createFont();
cellDatafont.setFontHeight((short)200);
cellDatafont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
cellDataStyle.setFont(cellDatafont);
cellDataStyle.setBorderBottom((short)1);
cellDataStyle.setBorderLeft((short)1);
cellDataStyle.setBorderRight((short)1);
cellDataStyle.setBorderTop((short)1);
cellDataStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cellDataStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//新增一個檢測資訊表
HSSFSheet sheet = wb.createSheet("檢測基本資訊表");
sheet.setDefaultColumnWidth(20);
sheet.setDefaultRowHeightInPoints(17);
//建立一行
HSSFRow row = sheet.createRow((int) 0);
// 樣式
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//字型:
org.apache.poi.hssf.usermodel.HSSFFont font = wb.createFont();
//合併網格
CellRangeAddress region = new CellRangeAddress(0, 0,0, 10);
sheet.addMergedRegion(region);
RegionUtil.setBorderBottom(1, region, sheet,wb); // 下邊框
RegionUtil.setBottomBorderColor(HSSFColor.BLACK.index, region, sheet, wb);
CellRangeAddress region1 = new CellRangeAddress(1,2,0,0);
sheet.addMergedRegion(region1);
RegionUtil.setBorderBottom(1, region1, sheet,wb); // 下邊框
RegionUtil.setBottomBorderColor(HSSFColor.BLACK.index, region1, sheet, wb);
region = new CellRangeAddress(1,2,1,1);
sheet.addMergedRegion(region);
region = new CellRangeAddress(1,2,2,2);
sheet.addMergedRegion(region);
region = new CellRangeAddress(1,2,3,3);
sheet.addMergedRegion(region);
region = new CellRangeAddress(1,2,4,4);
sheet.addMergedRegion(region);
region = new CellRangeAddress(1,1,5,10);
sheet.addMergedRegion(region);
//基本檢測資訊表 設定表頭
HSSFCell cell = row.createCell(0);
style.setFillBackgroundColor(HSSFColor.BLUE.index);
cell.setCellValue("檢測基本資訊表");
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor((short)18);
font.setFontHeight((short)250);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font);
cell.setCellStyle(style);
style= wb.createCellStyle();
font = wb.createFont();
row = sheet.createRow(1);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeight((short)250);
style.setFont(font);
title = new String[]{"開始日期","檢測人","檢測儀器","檢測背景","儀器序列","氣象五引數"};
for( int j1=0;j1<6;j1++){
cell = row.createCell(j1);
cell.setCellValue(title[j1]);
cell.setCellStyle(style);
}
title = new String[]{"溫度(°C)","溼度(%RH)","氣壓(hPa)","風向","風速(m/s)","備註"};
row = sheet.createRow(2);
for( int j1 =0;j1<11;j1++){
cell = row.createCell(j1);
cell.setCellStyle(style);
if( j1>4){
cell.setCellValue(title[j1-5]);
}
}
//插入資料
tableData = data.get(testBaseInforTableName);
font = wb.createFont();
font.setFontHeight((short)150);
style.setFont(font);
for(Map<String,Object> columnData : tableData){
row = sheet.createRow(tableMarginRow+3);
for( int j1=0;j1<11;j1++){
cell = row.createCell(j1);
cell.setCellValue((String)columnData.get(testBaseInforTaleTitle[j1]));
cell.setCellStyle(cellDataStyle);
}
}
//儀器校準資訊 設定表頭
tableMarginRow = tableData.size()+4;
region = new CellRangeAddress(tableMarginRow,tableMarginRow,0,11);
sheet.addMergedRegion(region);
//region = new CellRangeAddress(tableMarginRow+4,tableMarginRow+4,0,11);
// sheet.addMergedRegion(region);
row = sheet.createRow(tableMarginRow);
cell = row.createCell(0);
style = wb.createCellStyle();
font = wb.createFont();
font.setFontHeight((short)250);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor((short)15);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
cell.setCellValue("儀器校準資訊");
cell.setCellStyle(style);
title = new String[]{"儀器序列","校準日期","校準人員","標準氣理論濃度","標準氣實際濃度",
"計數值","儀器校準值","響應時間","漂移校準時間","計數值","漂移校準PPM","備註"};
tableMarginRow = tableMarginRow+1;
row = sheet.createRow((int) tableMarginRow);
style= wb.createCellStyle();
font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeight((short)180);
style.setFont(font);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
for(int j1=0;j1<12;j1++){
cell = row.createCell(j1);
cell.setCellValue(title[j1]);
cell.setCellStyle(style);
}
//新增資料
tableData = data.get(instructionTestTableName);
for(Map<String,Object> columnData : tableData){
row = sheet.createRow(++tableMarginRow);
for( int j1=0;j1<12;j1++){
cell = row.createCell(j1);
if( columnData.get(instructionTestTableTitle[j1]) instanceof Integer ){
cell.setCellValue(String.valueOf(columnData.get(instructionTestTableTitle[j1])));
}
else{
cell.setCellValue((String)columnData.get(instructionTestTableTitle[j1]));
}
cell.setCellStyle(cellDataStyle);
}
}
//新增檢測資訊sheet
sheet = wb.createSheet("檢測資訊表");
sheet.setDefaultColumnWidth(20);
sheet.setDefaultRowHeightInPoints(20);
title= new String[]{"裝置","區域","裝置","標籤號","擴充套件號","位置描述","元件型別","元件子型別","洩漏閾值","最小停留時間(秒)",
"結束時間","檢測儀器","檢測值","維修日期","維修人員","維修措施","複檢日期","複檢值","是否修復"};
row = sheet.createRow(0);
for( int j1 = 0;j1<title.length;j1++){
cell = row.createCell(j1);
cell.setCellStyle(style);
cell.setCellValue(title[j1]);
}
//新增資料
tableMarginRow = 0;
tableData = data.get(testInforTableName);
for(Map<String,Object> columnData : tableData){
row = sheet.createRow(++tableMarginRow);
for( int j1=0;j1<title.length;j1++){
cell = row.createCell(j1);
if( columnData.get(copyInforTableTitle[j1]) != null ){
if( columnData.get(copyInforTableTitle[j1]) instanceof Integer ){
cell.setCellValue(String.valueOf(columnData.get(copyInforTableTitle[j1])));
}
else{
cell.setCellValue((String)columnData.get(copyInforTableTitle[j1]));
}
}
cell.setCellStyle(cellDataStyle);
}
}
//寫出檔案
FileOutputStream out = null ;
try {
out = new FileOutputStream(fileName);
wb.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
感謝讀者的閱讀
相關文章
- excel生成單元格帶下拉選項的模板 + 資料匯入Excel
- 使用phpoffice/phpspreadsheet實現載入excel模板生成excelPHPExcel
- 【轉載】ALV的Excel方式顯示缺少模板Excel
- excel模板資料填充 :tablefillExcel
- post方法下載excelExcel
- 一個後臺靜態模板,自帶excel,print,cvsExcel
- 非常不錯的站長素材下載類網站原始碼 帶織夢模板網站原始碼
- django下載excel,使用django-excel外掛DjangoExcel
- SpringBoot專案Excel下載功能Spring BootExcel
- php匯入功能,模板下載PHP
- 地圖下載器 002 根據下載範圍獲取要下載的瓦片資訊地圖
- 免下載就能用的主圖模板,免費分享模板使用教程!
- Java 匯入資料到Excel並提供檔案下載介面JavaExcel
- csv和excel讀取和下載Excel
- ajax請求下載excel檔案Excel
- 前端方式實現excel下載前端Excel
- js實現txt/excel檔案下載JSExcel
- ajax 下載Excel 新增回撥函式Excel函式
- vue實現Excel檔案的上傳與下載VueExcel
- pyqt5的下載進度條 實現模板QT
- Excel不夠用?Smartbi的Excel融合分析帶你飛Excel
- Downie 4視訊下載軟體 Mac最好用的視訊下載軟體Mac
- 利用python爬取丁香醫生上新型肺炎資料,並下載到本地,附帶經緯度資訊Python
- BI資料視覺化報表模板分享,附免費下載方法視覺化
- Maatwebsite\Excel 讀取帶公式的excel檔案得到值呢?WebExcel公式
- 帶格式寫入excelExcel
- 網頁特效,網頁模板,pdf下載 - IT書包網頁特效
- 做不好資料視覺化不丟人,去奧威BI下載模板視覺化
- 一鍵下載你的youtube視訊
- Android 視訊教程下載Android
- VidiCable 視訊下載工具
- Spring Boot Web應用程式下載Excel檔案 - simplesolutionSpring BootWebExcel
- Servlet各版本的web.xml頭部資訊-模板ServletWebXML
- 視訊下載終極利器!簡單粗暴,支援全網視訊下載!
- 分享一個 Excel 格式的功能測試用例模板Excel
- 【LaTeX應用】圖書排版、PPT排版模板下載
- java匯出Excel定義匯出模板JavaExcel
- Excel模板匯出之動態匯出Excel