當前位置:首頁 » 編程語言 » javapoi單元格合並單元格

javapoi單元格合並單元格

發布時間: 2022-06-04 21:42:21

1. java poi合並單元表格(求幫助啊)

/**
*
*@paramcontext
*@paramdictionary
*@paramrows數據行
*@paramfileName文件名
*@paramfields列名
*@paramfieldsName欄位名
*/
privatevoidoutExcelFile(HttpContextcontext,
IContextDictionarydictionary,DataRowCollectionsrows,
StringfileName,List<String>fields,List<String>fieldsName){
intcellSize=fields.size();
if(cellSize==0){
LogManager.debug("沒有指定列頭信息,無法導出Excel文件!");
return;
}
//============創建樣式start
HSSFWorkbookworkbook=newHSSFWorkbook();
HSSFSheetsheet=workbook.createSheet();
//列頭字體樣式
HSSFFontheaderFont=workbook.createFont();
headerFont.setFontName("宋體");
headerFont.setFontHeightInPoints((short)12);
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//列頭樣式
HSSFCellStyleheaderStyle=workbook.createCellStyle();
headerStyle.setFont(headerFont);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
headerStyle.setLocked(true);
headerStyle.setWrapText(true);
//標題樣式
HSSFFonttitleFont=workbook.createFont();
titleFont.setFontName("宋體");
titleFont.setFontHeightInPoints((short)15);
titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

HSSFCellStyletitleStyle=workbook.createCellStyle();
titleStyle.setFont(titleFont);
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
titleStyle.setLocked(true);
titleStyle.setWrapText(true);

//普通單元格字體樣式
HSSFFontcellFont=workbook.createFont();
cellFont.setFontName("宋體");
cellFont.setFontHeightInPoints((short)12);
//普通單元格樣式
HSSFCellStylecellStyle=workbook.createCellStyle();
cellStyle.setFont(cellFont);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);//左右居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
cellStyle.setLocked(true);
cellStyle.setWrapText(true);
//============創建樣式end

//設置序號列、列寬和標題行start
HSSFRowtitleRow=sheet.createRow(0);
titleRow.setHeightInPoints(50);
HSSFCelltitleCell=titleRow.createCell(0);
titleCell.setCellValue(newHSSFRichTextString(fileName));
titleCell.setCellStyle(titleStyle);
//sheet.addMergedRegion(newRegion(0,(short)0,0,(short)cellSize));//合並單元格--方法過時
//CellRangeAddress起始行結束行起始列結束列
sheet.addMergedRegion(newCellRangeAddress(0,0,(short)0,(short)cellSize));//合並單元格

//顯示總的數據個數start
HSSFRowcountRow=sheet.createRow(1);
countRow.setHeightInPoints(40);
HSSFCellcountCell=countRow.createCell(0);
countCell.setCellValue(newHSSFRichTextString("共計專項檢查("+rows.size()+")項"));
countCell.setCellStyle(headerStyle);
sheet.addMergedRegion(newCellRangeAddress(1,1,(short)0,(short)cellSize));//合並單元格
//顯示總的數據個數end

HSSFRowheaderRow=sheet.createRow(2);
headerRow.setHeightInPoints(35);
HSSFCellheaderCell=null;

//序號
intstartIndex=0;
headerCell=headerRow.createCell(0);
sheet.setColumnWidth(0,2000);
headerCell.setCellValue(newHSSFRichTextString("序號"));

headerCell.setCellStyle(headerStyle);
startIndex++;

//列頭
for(inti=0;i<cellSize;i++){
sheet.setColumnWidth(startIndex+i,7000);
headerCell=headerRow.createCell(startIndex+i);
headerCell.setCellValue(newHSSFRichTextString(fields.get(i)));
headerCell.setCellStyle(headerStyle);
}
//設置序號列、列寬和標題行end

//文件正文start
introwNum=1;
introwIndex=0;
HSSFRowrow=null;
List<Integer[]>cellRangeLst=newArrayList<Integer[]>(0);
Integer[]arr=null;
intl=0;
StringorgName="";
for(intj=2;j<rows.size()+2;j++){//循環行
DataRowdataRow=rows.get(rowIndex);//對應資料庫欄位
HSSFCellcell=null;
row=sheet.createRow(j+1);
row.setHeightInPoints(55);
//序號
cell=row.createCell(0);
cell.setCellValue(rowNum++);
cell.setCellStyle(cellStyle);

if(StringHelper.isNullOrEmpty(orgName)){
arr=newInteger[2];
arr[0]=j+1;
l=j+1;
orgName=dataRow.getString("ORGNAME");
}else{
if(!orgName.equals(dataRow.getString("ORGNAME"))){
arr[1]=j;
cellRangeLst.add(arr);
sheet.addMergedRegion(newCellRangeAddress(l,j,1,1));
arr=newInteger[2];
l=j+1;
orgName=dataRow.getString("ORGNAME");
}

if(rowIndex==rows.size()-1){
arr[1]=j+1;
cellRangeLst.add(arr);
sheet.addMergedRegion(newCellRangeAddress(l,j+1,1,1));
}
}

for(intk=0;k<cellSize;k++){
cell=row.createCell(k+startIndex);
Stringcolumn=fieldsName.get(k);//對應資料庫欄位
Stringvalue="";
if("APSJ".equals(column)){
value=getAPSJValue(dataRow.getString(column));
}else{
value=dataRow.getString(column);
}
cell.setCellValue(newHSSFRichTextString(value));
cell.setCellStyle(cellStyle);
}
rowIndex++;
}
//文件正文end

//for(Integer[]te:cellRangeLst){
//sheet.addMergedRegion(newCellRangeAddress(te[0],te[1],1,1));//合並處室單元格
//}

//下載
HttpServletResponseresponse=context.getResponse();

response.setContentType("application/x-download;charset=UTF-8");
Stringtitle="export";
try{
title=java.net.URLEncoder.encode(fileName,"UTF-8");
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}
response.addHeader("Content-Disposition","attachment;filename="+title+".xls");
try{
OutputStreamout=response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
}catch(IOExceptione){
e.printStackTrace();
}
}

//參考一下吧

2. poi excel 怎麼合並單元格

3. java poi xwpf操作word生成一個表格怎麼合並單元格,求大神指導!

有個最簡單,不用使用任何 poi 或其他第三方類庫的方法生成 excel ,或 word ,

你把一個生成好的 word 例子用文件另存為 xml , docx之類,然後你再直接用記事本去打開這個 docx ,你就會發現是 xml 格式,

這個時候,你在程序裡面就直接用普通代碼生成這個 xml 就可以了。

4. 有關java通過poi處理excle中合並單元格的問題

HSSFSheet.getNumMergedRegions取合並格個數,HSSFSheet.getMergedRegionAt取第幾個合並格的合並區域
循環幾下就可取到當前格的合並行數

5. java excel 怎麼合並大的單元格

建議使用庫來實現,spire.xls for java的單元格合並教程文章你可以參考看看

6. java poi怎麼讀取Excel中合並單元格的值

獲取合並單元格的值 @param sheet @param row @param column @return。

7. poi Java生成excel合並單元格後字體居中

我想是合並以後再做以下處理吧。
取得現有式樣。
調用setAlignment,重新設置居中:
CellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
再把式樣設置到cell中:
HSSFCell.setCellStyle(CellStyle);

熱點內容
培訓php還是java 發布:2024-04-28 07:10:49 瀏覽:918
dal數據訪問 發布:2024-04-28 07:09:00 瀏覽:521
python判斷伺服器是內網地址 發布:2024-04-28 07:07:17 瀏覽:782
手機qq影音加密 發布:2024-04-28 07:05:27 瀏覽:780
多次編譯仍然啟動不了 發布:2024-04-28 06:33:17 瀏覽:598
絕地求生怎麼調伺服器2020 發布:2024-04-28 06:28:14 瀏覽:802
虛擬主機和輕雲伺服器 發布:2024-04-28 05:34:20 瀏覽:774
電腦伺服器的品牌型號怎麼查 發布:2024-04-28 04:33:06 瀏覽:138
文件上傳ui 發布:2024-04-28 04:07:14 瀏覽:483
我的世界pe登伺服器 發布:2024-04-28 04:05:58 瀏覽:672