java poi 只能创建?,JAVA POI创建多个sheet的Excel文件,及多个sheet也创建失败原因
最近寫一個生成多個sheet頁的表格,正好遇到了sheet只有一個的問題,拿出來跟大家分享下
解決問題直接下拉!
import java.io.IOException;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
/**
* 解析excel文件
* @author guosk
*/
public class WriteExcelUtils {
/**
* 輸出Excel文檔
* @param workbook
* @param resources 源數(shù)據(jù)
* @param headerNames 表頭
* @param sheetName 表格名
* @param columnNum 列數(shù)量
* @param sheetNum 頁碼(sheet頁碼)
* @throws IOException
*/
public static void writeExcel(HSSFWorkbook workbook,
Listresources,
String[] headerNames,
String sheetName,
Integer columnNum,
Integer sheetNum) throws IOException {
// 創(chuàng)建表格
HSSFSheet sheet = workbook.createSheet();
sheet.setDefaultRowHeightInPoints(13);//默認寬度
workbook.setSheetName(sheetNum, sheetName);
// 設(shè)置列寬,根據(jù)
for(int i=0; i<=columnNum; i++){
sheet.setColumnWidth(i, 6000);
}
/*
* 創(chuàng)建合并區(qū)域
* CellRangeAddress(int 首行, int 最后一行, int 首列, int 最后一列);
*/
CellRangeAddress add = new CellRangeAddress(0, 0, 0, columnNum);
// 將創(chuàng)建的合并區(qū)域設(shè)置到表格中.
sheet.addMergedRegion(add);
// 創(chuàng)建行
Row header = sheet.createRow(0);
// 創(chuàng)建單元格. 合并后的單元格,編號合并.
//設(shè)置樣式
CellStyle titleStyle = workbook.createCellStyle();
Font titlefont = workbook.createFont();
titlefont.setFontName("黑體");
//titlefont.setColor(IndexedColors.VIOLET.index);
titlefont.setFontHeightInPoints((short)20);
titlefont.setBold(true);
titleStyle.setFont(titlefont);
titleStyle.setAlignment(HorizontalAlignment.CENTER);
Cell c = header.createCell(0);
c.setCellValue(sheetName);
c.setCellStyle(titleStyle);
c = header.createCell(columnNum);
// 編寫表頭
// 定義表頭的樣式
CellStyle headerStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontName("宋體");
//font.setColor(IndexedColors.VIOLET.index);
font.setFontHeightInPoints((short)16);
headerStyle.setFont(font);
headerStyle.setAlignment(HorizontalAlignment.CENTER);
// 設(shè)置單元格樣式
Row headerRow = sheet.createRow(1);
for (int i = 0; i < headerNames.length; i++) {
Cell cell = headerRow.createCell(i);
// 設(shè)置單元格樣式
cell.setCellStyle(headerStyle);
cell.setCellValue(headerNames[i]);
}
// 設(shè)置表格數(shù)據(jù)的樣式
CellStyle bodyStyle = workbook.createCellStyle();
Font bodyFont = workbook.createFont();
bodyFont.setFontName("微軟雅黑");
//bodyFont.setColor(IndexedColors.BLUE.index);
bodyFont.setFontHeightInPoints((short)12);
bodyStyle.setFont(bodyFont);
// 編輯表格體數(shù)據(jù)
for (int i = 0; i < resources.size(); i++) {
// 獲取行數(shù)據(jù)
String[] temp = resources.get(i);
// 創(chuàng)建行
Row bodyRow = sheet.createRow(i + 2);
for (int cellNum = 0; cellNum < temp.length; cellNum++) {
Cell bodyCell = bodyRow.createCell(cellNum);
bodyCell.setCellStyle(bodyStyle);
bodyCell.setCellValue(temp[cellNum]);
}
}
sheet.getRow(0).setHeightInPoints(24);
sheet.getRow(1).setHeightInPoints(20);
}
}
下面說一下多個sheet頁創(chuàng)建失敗的原因:
當你在寫完這個方法之后如果直接寫入write(out)的話,就是永遠只創(chuàng)建一個sheet
所以要在你執(zhí)行多次之后再寫出write(out)才可以
總結(jié)
以上是生活随笔為你收集整理的java poi 只能创建?,JAVA POI创建多个sheet的Excel文件,及多个sheet也创建失败原因的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php rmdir 返回值,php通过r
- 下一篇: opencv 腐蚀 matlab,Ope