java匯出Excel合併單元格
網上java匯出excel表格併合並單元格的資料不完全,我整理了一份,並親測能用,附截圖。
①java匯出excel用到POI所有jar包,大家可以直接到下面地址下載點選開啟連結
②模擬資料類
package org; public class WorkSheetDetail { //工作內容 private String workCtx; // 用工人總數 工日數 = gwnNum+tmnNum private Float totalHumanDays; //普工用工數 1-4小時為半天,4-8小時為一天;120每天 private Integer gwnNum; //技工用工數 1-4小時為半天,4-8小時為一天;160每天 private Integer tmnNum; // 單價(元) private Float unitPrice; // 金額(元) = gwnNum*120+tmnNum+160 private Float unitAmount; // 備註 private String notes; public WorkSheetDetail(String workCtx, Float totalHumanDays, Integer gwnNum, Integer tmnNum, Float unitPrice, Float unitAmount, String notes) { super(); this.workCtx = workCtx; this.totalHumanDays = totalHumanDays; this.gwnNum = gwnNum; this.tmnNum = tmnNum; this.unitPrice = unitPrice; this.unitAmount = unitAmount; this.notes = notes; } public String getWorkCtx() { return workCtx; } public void setWorkCtx(String workCtx) { this.workCtx = workCtx; } public Float getTotalHumanDays() { return totalHumanDays; } public void setTotalHumanDays(Float totalHumanDays) { this.totalHumanDays = totalHumanDays; } public Integer getGwnNum() { return gwnNum; } public void setGwnNum(Integer gwnNum) { this.gwnNum = gwnNum; } public Integer getTmnNum() { return tmnNum; } public void setTmnNum(Integer tmnNum) { this.tmnNum = tmnNum; } public Float getUnitPrice() { return unitPrice; } public void setUnitPrice(Float unitPrice) { this.unitPrice = unitPrice; } public Float getUnitAmount() { return unitAmount; } public void setUnitAmount(Float unitAmount) { this.unitAmount = unitAmount; } public String getNotes() { return notes; } public void setNotes(String notes) { this.notes = notes; } }
③java匯出excel方法
④main方法package org; import java.io.FileOutputStream; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFHeader; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.util.CellRangeAddress; public class ExportExcel { public void getValue(List<WorkSheetDetail> userList,FileOutputStream fout){ try{ //1.建立工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); //1.1建立合併單元格物件 CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,7);//起始行,結束行,起始列,結束列 CellRangeAddress callRangeAddress1 = new CellRangeAddress(1,1,0,7);//起始行,結束行,起始列,結束列 //班組與時間start CellRangeAddress callRangeAddress20 = new CellRangeAddress(2,2,0,2);//起始行,結束行,起始列,結束列 CellRangeAddress callRangeAddress21 = new CellRangeAddress(2,2,3,4);//起始行,結束行,起始列,結束列 CellRangeAddress callRangeAddress22 = new CellRangeAddress(2,2,5,7);//起始行,結束行,起始列,結束列 //班組與時間end //標題 CellRangeAddress callRangeAddress31 = new CellRangeAddress(3,4,0,0);//起始行,結束行,起始列,結束列 CellRangeAddress callRangeAddress32 = new CellRangeAddress(3,4,1,1);//起始行,結束行,起始列,結束列 CellRangeAddress callRangeAddress33 = new CellRangeAddress(3,4,2,2);//起始行,結束行,起始列,結束列 CellRangeAddress callRangeAddress34 = new CellRangeAddress(3,3,3,4);//起始行,結束行,起始列,結束列 CellRangeAddress callRangeAddress35 = new CellRangeAddress(3,4,5,5);//起始行,結束行,起始列,結束列 CellRangeAddress callRangeAddress36 = new CellRangeAddress(3,4,6,6);//起始行,結束行,起始列,結束列 CellRangeAddress callRangeAddress37 = new CellRangeAddress(3,4,7,7);//起始行,結束行,起始列,結束列 //金額 CellRangeAddress callRangeAddressnumber1 = new CellRangeAddress(userList.size()+5,userList.size()+5,0,2);//起始行,結束行,起始列,結束列 CellRangeAddress callRangeAddressnumber2 = new CellRangeAddress(userList.size()+5,userList.size()+5,3,7);//起始行,結束行,起始列,結束列 //負責人 CellRangeAddress callRangeAddressPersion1 = new CellRangeAddress(userList.size()+6,userList.size()+6,0,2);//起始行,結束行,起始列,結束列 CellRangeAddress callRangeAddressPersion2 = new CellRangeAddress(userList.size()+6,userList.size()+6,3,4);//起始行,結束行,起始列,結束列 CellRangeAddress callRangeAddressPersion3 = new CellRangeAddress(userList.size()+6,userList.size()+6,5,7);//起始行,結束行,起始列,結束列 //說明 CellRangeAddress callRangeAddressinfo = new CellRangeAddress(userList.size()+7,userList.size()+7,0,7);//起始行,結束行,起始列,結束列 CellRangeAddress callRangeAddressinfo1 = new CellRangeAddress(userList.size()+8,userList.size()+8,0,7);//起始行,結束行,起始列,結束列 CellRangeAddress callRangeAddressinfo2 = new CellRangeAddress(userList.size()+9,userList.size()+9,0,7);//起始行,結束行,起始列,結束列 //部專案經理部 HSSFCellStyle headStyle = createCellStyle(workbook,(short)10,false,true); //派工單 HSSFCellStyle erStyle = createCellStyle(workbook,(short)13,true,true); //班組和時間 HSSFCellStyle sanStyle = createCellStyle(workbook,(short)10,false,false); //標題樣式 HSSFCellStyle colStyle = createCellStyle(workbook,(short)10,true,true); //內容樣式 HSSFCellStyle cellStyle = createCellStyle(workbook,(short)10,false,true); //2.建立工作表 HSSFSheet sheet = workbook.createSheet("派單"); //2.1載入合併單元格物件 sheet.addMergedRegion(callRangeAddress); sheet.addMergedRegion(callRangeAddress1); sheet.addMergedRegion(callRangeAddress20); sheet.addMergedRegion(callRangeAddress21); sheet.addMergedRegion(callRangeAddress22); sheet.addMergedRegion(callRangeAddress31); sheet.addMergedRegion(callRangeAddress32); sheet.addMergedRegion(callRangeAddress33); sheet.addMergedRegion(callRangeAddress34); sheet.addMergedRegion(callRangeAddress35); sheet.addMergedRegion(callRangeAddress36); sheet.addMergedRegion(callRangeAddress37); sheet.addMergedRegion(callRangeAddressnumber1); sheet.addMergedRegion(callRangeAddressnumber2); sheet.addMergedRegion(callRangeAddressPersion1); sheet.addMergedRegion(callRangeAddressPersion2); sheet.addMergedRegion(callRangeAddressPersion3); sheet.addMergedRegion(callRangeAddressinfo); sheet.addMergedRegion(callRangeAddressinfo1); sheet.addMergedRegion(callRangeAddressinfo2); //設定預設列寬 sheet.setDefaultColumnWidth(15); //3.建立行 //3.1建立頭標題行;並且設定頭標題 HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); //載入單元格樣式 cell.setCellStyle(headStyle); cell.setCellValue("xxxx專案部"); HSSFRow rower = sheet.createRow(1); HSSFCell celler = rower.createCell(0); //載入單元格樣式 celler.setCellStyle(erStyle); celler.setCellValue("派 工 單"); HSSFRow rowsan = sheet.createRow(2); HSSFCell cellsan = rowsan.createCell(0); HSSFCell cellsan1 = rowsan.createCell(3); HSSFCell cellsan2 = rowsan.createCell(5); //載入單元格樣式 cellsan.setCellStyle(sanStyle); cellsan.setCellValue("協作單位:x施工一堆"); cellsan1.setCellStyle(sanStyle); cellsan1.setCellValue(""); cellsan2.setCellStyle(sanStyle); cellsan2.setCellValue("時間:2017年 10月 20日"); //3.2建立列標題;並且設定列標題 HSSFRow row2 = sheet.createRow(3); String[] titles = {"序號","工作內容","用工總人數","工日數","","單價(元)","金額(元)","備註"};//""為佔位字串 for(int i=0;i<titles.length;i++) { HSSFCell cell2 = row2.createCell(i); //載入單元格樣式 cell2.setCellStyle(colStyle); cell2.setCellValue(titles[i]); } HSSFRow rowfour = sheet.createRow(4); String[] titlefour = {"普工用工數","技工用工數"}; for(int i=0;i<titlefour.length;i++) { HSSFCell cell2 = rowfour.createCell(i+3); //載入單元格樣式 cell2.setCellStyle(colStyle); cell2.setCellValue(titlefour[i]); } //4.操作單元格;將使用者列表寫入excel if(userList != null) { int i=1; for(int j=0;j<userList.size();j++) { //建立資料行,前面有兩行,頭標題行和列標題行 HSSFRow row3 = sheet.createRow(j+5); HSSFCell cell0 = row3.createCell(0); cell0.setCellStyle(cellStyle); cell0.setCellValue(i++); HSSFCell cell1 = row3.createCell(1); cell1.setCellStyle(cellStyle); cell1.setCellValue(userList.get(j).getWorkCtx()); HSSFCell cell2 = row3.createCell(2); cell2.setCellStyle(cellStyle); cell2.setCellValue(userList.get(j).getTotalHumanDays()); HSSFCell cell3 = row3.createCell(3); cell3.setCellStyle(cellStyle); cell3.setCellValue(userList.get(j).getGwnNum()); HSSFCell cell4 = row3.createCell(4); cell4.setCellStyle(cellStyle); cell4.setCellValue(userList.get(j).getTmnNum()); HSSFCell cell5 = row3.createCell(5); cell5.setCellStyle(cellStyle); cell5.setCellValue(userList.get(j).getTotalHumanDays()); HSSFCell cell6 = row3.createCell(6); cell6.setCellStyle(cellStyle); cell6.setCellValue(userList.get(j).getUnitAmount()); HSSFCell cell7= row3.createCell(7); cell7.setCellStyle(cellStyle); cell7.setCellValue(userList.get(j).getUnitPrice()); } } HSSFRow rownumber = sheet.createRow(userList.size()+5); HSSFCell cellnumber = rownumber.createCell(0); HSSFCell cellnumber1 = rownumber.createCell(3); //載入單元格樣式 cellnumber.setCellStyle(sanStyle); cellnumber.setCellValue("金額合計(大寫)"); cellnumber1.setCellStyle(sanStyle); cellnumber1.setCellValue("¥ 78 元; 大寫:柒拾捌元整"); HSSFRow rowpersion = sheet.createRow(userList.size()+6); HSSFCell cellpersion = rowpersion.createCell(0); HSSFCell cellpersion1 = rowpersion.createCell(3); HSSFCell cellpersion2 = rowpersion.createCell(5); //載入單元格樣式 cellpersion.setCellStyle(sanStyle); cellpersion.setCellValue("協作單位負責人:"); cellpersion1.setCellStyle(sanStyle); cellpersion1.setCellValue("經辦人:"); cellpersion2.setCellStyle(sanStyle); cellpersion2.setCellValue("部門負責人:"); HSSFRow rowinfo = sheet.createRow(userList.size()+7); HSSFCell cellinfo = rowinfo.createCell(0); cellinfo.setCellStyle(sanStyle); cellinfo.setCellValue("說明:1、本標工單一式兩聯,第一聯為派工人(工長)存根,第二聯用作結算。"); HSSFRow rowinfo1 = sheet.createRow(userList.size()+8); HSSFCell cellinfo1 = rowinfo1.createCell(0); cellinfo1.setCellStyle(sanStyle); cellinfo1.setCellValue("2、本標工單必須在用工當日籤認,否則不予認可;三日內交合同處彙總。"); HSSFRow rowinfo2 = sheet.createRow(userList.size()+9); HSSFCell cellinfo2 = rowinfo2.createCell(0); cellinfo2.setCellStyle(sanStyle); cellinfo2.setCellValue("3、工日數填寫精確到半個工日。"); //5.輸出 workbook.write(fout); // workbook.close(); //out.close(); }catch(Exception e) { e.printStackTrace(); } } /** * * @param workbook * @param fontsize * @return 單元格樣式 */ private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontsize,boolean flag,boolean flag1) { // TODO Auto-generated method stub HSSFCellStyle style = workbook.createCellStyle(); //是否水平居中 if(flag1){ style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 } style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 //建立字型 HSSFFont font = workbook.createFont(); //是否加粗字型 if(flag){ font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); } font.setFontHeightInPoints(fontsize); //載入字型 style.setFont(font); return style; } }
package org; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; public class MainOut { public static void main(String args[]){ //模擬部分資料 List<WorkSheetDetail> detail = new ArrayList<WorkSheetDetail>(); WorkSheetDetail d1 =new WorkSheetDetail("23",23f,43,34,243f,54f,"34"); WorkSheetDetail d2 =new WorkSheetDetail("23",23f,43,34,243f,54f,"34"); WorkSheetDetail d3 =new WorkSheetDetail("23",23f,43,34,243f,54f,"34"); WorkSheetDetail d4 =new WorkSheetDetail("23",23f,43,34,243f,54f,"34"); WorkSheetDetail d5 =new WorkSheetDetail("23",23f,43,34,243f,54f,"34"); detail.add(d1); detail.add(d2); detail.add(d3); detail.add(d4); detail.add(d5); try { FileOutputStream fout = new FileOutputStream("E:/students.xls"); new ExportExcel().getValue(detail, fout); fout.close(); } catch (Exception e) { e.printStackTrace(); } } }
⑤截圖
相關推薦
java匯出Excel合併單元格
網上java匯出excel表格併合並單元格的資料不完全,我整理了一份,並親測能用,附截圖。 ①java匯出excel用到POI所有jar包,大家可以直接到下面地址下載點選開啟連結 ②模擬資料類 package org; public class WorkSheetDet
JXL匯出EXCEL合併單元格
圖片 import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.servlet.h
POI匯出Excel--合併單元格
package com.test.util; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io
JXL匯出EXCEL合併單元格、文字對齊方式
這次能做出來,主要是感謝各位在網上提供的資料,在此記錄 匯出的結果是,大家看哈,若有可取之處可繼續 程式碼比較多,大家主要看哈我用紅色標註部分即可 --------------------------------------------------------------
Java-poi匯入excel合併單元格
要匯入的excel示例 一、Action @Action("/import-excel") public void toimport() { String root = ServletActionContext.getSer
POI匯出Excel設定單元格格式2--建立與設定Excel合併單元格
POI建立與設定Excel合併單元格 話不多說上栗子 //準備工作 XSSFWorkbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("sheet1"); XSSFCreationHelper creationHel
java中匯出excel設定單元格的樣式為數字格式
//新增帶有formatting的Number物件 jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##"); //設定數字格式 jxl.write.WritableCellFormat wcfN
讓我頭疼一下午的Excel合併單元格
Excel匯出常見問題 excel匯出其實不算什麼難事 在網上copy下模板程式碼,填充自己的業務資料,提供一個http介面基本就可以得到你要匯出的資料了。 但是,凡事都有例外,截止今天,excel匯出我遇到的主要是兩大類問題 1、大資料量的excel資料,比如幾十萬條甚至更多的資料匯出 2、因為excel中
poi框架匯出excel寫單元格遇到精度問題
背景: java系統,MySql資料庫,定義有些資料格式為Decimal(24,2),即最多整數22位,小數2位,或者Decimal(24,4),即最多整數20位,小數4位的數字。系統內部操作使用BigDecimal來記錄和操作這樣的資料,並無不妥,也不會丟失
Delphi 轉EXCEL 合併單元格解決方案
廢話少說先上傳程式碼,自己看吧! procedure TForm1.Button10Click(Sender: TObject); var FExcel:Variant; FWorkbook:Variant; FWorkSheet:Variant; XlsF
POI匯出Excel設定單元格格式
使用Apache的POI相關API匯出Excel設定單元格格式 栗子,一下各個程式碼之間的變數是通用的,要是在某個程式碼塊中找不到某個變數,則可以向上找尋 準備工作 InputStream = template//檔案輸入流 XSSFWorkbook wb = new XSSFW
Apache POI如何獲取Excel合併單元格的值
/** * 獲取合併單元格的值 * @param sheet * @param row * @param column * @return */ public String getMergedRegionValue(Sheet sheet ,in
Java poi操作合併單元格後寫入內容
今天做這個東西,開始一直顯示不出來,不知道是先寫還是先合併,發現都不行,網上找了下,好像沒什麼人注意這個問題,經過本人的實驗,發現poi合併單元格過後他的列數是不變的,比如你第一行第一列和第四列合併,第一行第五列和第八
java POI 實現合併單元格
合併單元格所使用的方法: sheet.addMergedRegion( CellRangeAddress cellRangeAddress ); CellRangeAddress 物件的構造
PHPExcel的自定義匯出及合併單元格
首先自定義匯出,我用的是一個下拉多選框的一個外掛,百度一下就可找到,為了樣式好看。如圖value值對應的是你資料庫中查出的欄位值,text對應的是你的表頭資訊。ok,然後我是通過GET把這倆個值傳到我們控制器的。引入匯出類,這個就不多說。然後就是查詢資料庫,把資料處理成一個二
Java匯出Excel表,POI 實現合併單元格以及列自適應寬度
//字型 HSSFFont font = workbook.createFont(); font.setFontName("仿宋_GB2312"); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗體顯示
java將資料匯出,帶有合併單元格的excel--jxls技術
jxls技術可以生成各種樣式的報表,非常好用,深深地喜歡上了這個。說起用這個還是比較有意思的,當時專案有個匯出表格的功能,但是沒能合併單元格,客戶不是很滿意,當時專案中大家都說弄不了,我想著自己網上查查吧,就查到了這個,試了一下午完成了,很有成就感哪,哈哈。
java poi匯出Excel表,合併單元格
其他參考文章: http://www.cnblogs.com/bmbm/archive/2011/12/08/2342261.html http://www.cnblogs.com/xuyuanjia/p/5886056.html 這是一個struts2的act
Java poi匯出設定 Excel某些單元格不可編輯
小白的總結,大神勿噴;需要轉載請說明出處,如果有什麼問題,歡迎留言 一、需求: 1、某一列 、某一行或某些單元格不可編輯,其他列可以編輯 二、期間遇到的問題 1、無法設定成不可編輯 2、設定為不可編輯,匯出後發現所有單元格均不可編輯; 原因:createCell();建立單元格後,單元
HSSFWorkbook——匯出excel,動態合併單元格
一 前言 開發中,對於匯出一個excel表格這樣的功能很常見,這裡談談我所知道的相關知識 二 需求 匯出某個套餐所關聯的所有專案的一個Excel表格 三 HSSFWorkbook //建立一個Excel檔案 HSSFWorkbook&