/**
*
- 功能描述:
- @Package: com.utils.excel.poiexcel
*/
package com.utils.excel.poiexcel;
import java.io.IOException;
import java.util.Date;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
*
- @ClassName: PoiExcelUtil.java
- @Description: 導出Excel採用HSSFWorkbook實現方式 excel爲03以前的版本,只支持.xls格式 提供了單sheet頁,多個sheet頁方法
- @deprecated 不建議使用此方法了,建議使用下邊的PoiSXSSFWorkbookExcel.java工具類
*/
public class PoiExcelUtil {
/**
*
* 導出Excel 簡單導出 單個sheet頁 數據量不大的可以使用
*
* @param sheetName
* sheet名稱
* @param title
* 標題
* @param content
* 內容
* @return wb
* HSSFWorkbook對象
*
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] content) {
// 第一步 創建一個HSSFWorkbook, 對應一個Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 聲明一個工作薄
// wb.createInformationProperties();
// wb.getDocumentSummaryInformation().setCompany("*****公司");
// SummaryInformation si = wb.getSummaryInformation();
// si.setAuthor("JACK"); //填加xls文件作者信息
// si.setApplicationName("導出程序"); //填加xls文件創建程序信息
// si.setLastAuthor("最後保存者信息"); //填加xls文件最後保存者信息
// si.setComments("JACK is a programmer!"); //填加xls文件作者信息
// si.setTitle("POI導出Excel"); //填加xls文件標題信息
// si.setSubject("POI導出Excel");//填加文件主題信息
// si.setCreateDateTime(new Date());
// 第二步, 在workbook中添加一個sheet,對應excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// sheet.setColumnWidth(2, 100);//設置某一個下標的列寬
// sheet.setDefaultColumnStyle(column, style);//設置某一列的默認樣式
// sheet.setDefaultColumnWidth(20);//設置所有列的列寬
// sheet.setColumnHidden(columnIndex, hidden);//設置某一列是否隱藏
// sheet.setDefaultRowHeight((short)(30*20));//設置行高60
// sheet.setDefaultRowHeightInPoints(30);//設置行高30
// 合併單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列
// CellRangeAddress region1=new CellRangeAddress(0, 0, 0, 2);
// CellRangeAddress region2=new CellRangeAddress(1, 1, 0, 2);
// sheet.addMergedRegion(region1);
// sheet.addMergedRegion(region2);
// 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制,參數爲行索引(excel的行),可以是0~65535之間的任何一個
int rownum = 0;
HSSFRow row = sheet.createRow(rownum);
// 第四步,創建單元格,並設置表頭 設置表頭居中
// 給表還是單元格還是列可以自己設置並獲取到需要的樣式titlestyle cellstyle
HSSFCellStyle titlestyle = getAndSetHSSFCellStyleHeader(wb);
// 聲明列對象,參數爲列索引,可以是0~255之間的任何一個
HSSFCell cell;
// 創建標題,此時row=0,即第一行
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(titlestyle);
}
// 第二行
rownum++;
HSSFRow row02 = sheet.createRow(rownum);
HSSFCell cell_row02 = row02.createCell(0);
cell_row02.setCellValue("總數:" + (content.length) + ",導出時間:" + new Date().toLocaleString());
// 設置小標題樣式
cell_row02.setCellStyle(titlestyle);
// 創建內容 第三行
for (int i = 0; i < content.length; i++) {
String str = ",";
row = sheet.createRow(i + 2);
for (int j = 0; j < content[i].length; j++) {
// 將內容按照順序賦給對應的列對象
row.createCell(j).setCellValue(String.valueOf(content[i][j]));
}
}
return wb;
}
/**
*
* 導出Excel 多個sheet頁 每個sheet頁表頭內容不同
*
* @param sheetNum
* sheet頁
* @param sheetTitle
* sheet頁名稱
* @param title
* 標題
* @param List<List<String>> result
* 導出數據內容
* @return wb
* HSSFWorkbook對象
* @throws IOException
*
*/
public static HSSFWorkbook getsheetsHSSFWorkbook(HSSFWorkbook wb, int sheetNum, String sheetTitle, String[] title,
List<List<String>> result) {
HSSFSheet sheet = wb.createSheet();
wb.setSheetName(sheetNum, sheetTitle);
int rownum = 0;
HSSFRow row = sheet.createRow(rownum);
// 設置並獲取到需要的樣式
HSSFCellStyle titlestyle = getAndSetHSSFCellStyleHeader(wb);
// 聲明列對象,參數爲列索引,可以是0~255之間的任何一個
HSSFCell cell;
// 創建標題,此時row=0,即第一行
for (int i = 0; i < title.length; i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(titlestyle);
}
/*// 第二行
rownum++;
HSSFRow row02 = sheet.createRow(rownum);
HSSFCell cell_row02 = row02.createCell(0);
cell_row02.setCellValue("總數:" + (result.size()) + ",導出時間:" + new Date().toLocaleString());
// 設置小標題樣式
cell_row02.setCellStyle(titlestyle);*/
// 創建內容 第三行
// 遍歷集合數據,產生數據行
if (result != null) {
int index = 1;
for (List<String> m : result) {
row = sheet.createRow(index);
int cellIndex = 0;
for (String str : m) {
row.createCell((short) cellIndex).setCellValue(str);
cellIndex++;
}
index++;
}
}
return wb;
}
/**
*
* 導出Excel 數據量過多的話放入多個sheet頁中
* 每個sheet頁目前設置爲60000
*
* @param title
* 標題
* @param List<List<String>> result
* 導出數據內容
* @return wb
* HSSFWorkbook對象
* @throws IOException
*
*/
public static HSSFWorkbook getManySheets(String[] title, List<List<String>> result) {
HSSFWorkbook wb = new HSSFWorkbook();
// sheet名
String sheetTitle = "業務清單";
int sheetNum = 0;// 記錄額外創建的sheet數量
HSSFSheet sheet = wb.createSheet(sheetTitle + sheetNum);
// wb.setSheetName(sheetNum, sheetTitle+sheetNum);
int rownum = 0;
HSSFRow row = sheet.createRow(rownum);
// 設置並獲取到需要的樣式
HSSFCellStyle titlestyle = getAndSetHSSFCellStyleHeader(wb);
HSSFCell cell;
// 創建標題,此時row=0,即第一行
for (int j = 0; j < title.length; j++) {
cell = row.createCell(j);
cell.setCellValue(title[j]);
cell.setCellStyle(titlestyle);
}
// 遍歷集合數據,創建excel內容,產生數據行
if (result != null) {
List<String> m = null;
for (int i = 0; i < result.size(); i++) {
if ((i + 1) % 60000 == 0) {
sheetNum++;
sheet = wb.createSheet(sheetTitle + sheetNum);
row = sheet.createRow(0);
// 聲明列對象,參數爲列索引,可以是0~255之間的任何一個
// 創建標題,此時row=0,即第一行
for (int j = 0; j < title.length; j++) {
cell = row.createCell(j);
cell.setCellValue(title[j]);
cell.setCellStyle(titlestyle);
}
}
row = sheet.createRow((i + 1) - (sheetNum * 60000)+sheetNum);
int cellIndex = 0;
m = result.get(i);
for (String str : m) {
row.createCell((short) cellIndex).setCellValue(str);
cellIndex++;
}
}
}
return wb;
}
/**
* 獲取並設置header樣式
*/
private static HSSFCellStyle getAndSetHSSFCellStyleHeader(HSSFWorkbook wb) {
HSSFCellStyle hssfCellStyle = wb.createCellStyle();
// 創建一個居中格式
hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 設置字體
HSSFFont font = wb.createFont();
font.setFontName("楷體");
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
hssfCellStyle.setFont(font);
// 創建邊框對象
HSSFCellStyle setBorder = wb.createCellStyle();
// 設置自動換行
setBorder.setWrapText(true);
return hssfCellStyle;
}
}