java生成excel表格
1.需求
將從資料庫查詢出來的資料,以報表生成到本地
2實現
import java.io.File;
import java.io.FileOutputStream;
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;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.stereotype.Service;import com.jf.cloud.model.common.PoiReportExcel;
import com.jf.cloud.service.intf.common.ExportService;
@SuppressWarnings("deprecation")
@Service
public class ExportServiceImpl implements ExportService {// 報表匯出 匯出成功返回地址,否則返回null
@Override
public String generateExcel(String sheetName, String titleName,
int[] columnWidth, String fileName, String[] columnName,
List<List<String>> dataList) {if (columnWidth.length != columnName.length) {
return "列數和表頭數不相等";
}// 建立一個webbook,對應一個Excel檔案
HSSFWorkbook wb = new HSSFWorkbook();// 在webbook中新增一個sheet,對應Excel檔案中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);// 設定列數
int columnNumber = columnWidth.length;// 設定每一列的寬度
for (int i = 0; i < columnWidth.length; i++) {for (int j = 0; j <= i; j++) {
if (i == j) {
sheet.setColumnWidth(i, columnWidth[j] * 256); // 單獨設定每列的寬
}
}
}// 設定標題
HSSFRow row1 = sheet.createRow(0);// 設定標題的高度
row1.setHeightInPoints(50);// 建立標題單元格樣式以及字型樣式
HSSFCellStyle style = wb.createCellStyle();style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// 建立字型樣式
HSSFFont headerFont1 = (HSSFFont) wb.createFont();// 字型加粗
headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 設定字型型別
headerFont1.setFontName("黑體");// 設定字型大小
headerFont1.setFontHeightInPoints((short) 15);// 為標題樣式設定字型樣式
style.setFont(headerFont1);// 建立第一列
HSSFCell cell1 = row1.createCell(0);// 合併所有列並居中
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, columnNumber - 1));// 設定值標題
cell1.setCellValue(titleName);// 設定標題樣式
cell1.setCellStyle(style);// 設定表頭
HSSFRow row = sheet.createRow(1);// 設定表頭高度
row.setHeightInPoints(37);// 建立表頭單元格樣式 以及表頭的字型樣式
HSSFCellStyle style1 = wb.createCellStyle();// 設定自動換行
style1.setWrapText(true);style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 居中格式
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);style1.setBottomBorderColor(HSSFColor.BLACK.index);
style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
// 建立字型樣式
HSSFFont headerFont = (HSSFFont) wb.createFont();// 字型加粗
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 設定字型型別
headerFont.setFontName("黑體");// 設定字型大小
headerFont.setFontHeightInPoints((short) 10);// 為標題樣式設定字型樣式
style1.setFont(headerFont);// 建立表頭的列
for (int i = 0; i < columnNumber; i++) {
HSSFCell cell = row.createCell(i);cell.setCellValue(columnName[i]);
cell.setCellStyle(style1);
}// 建立單元格,並設定值 `
for (int i = 0; i < dataList.size(); i++) {
// 建立行
row = sheet.createRow(i + 2);// 設定為自動換行並居中
HSSFCellStyle lineBreak = wb.createCellStyle();// 設定自動換行
lineBreak.setWrapText(true);// 建立一個居中格式
lineBreak.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 設定邊框
lineBreak.setBottomBorderColor(HSSFColor.BLACK.index);lineBreak.setBorderBottom(HSSFCellStyle.BORDER_THIN);
lineBreak.setBorderLeft(HSSFCellStyle.BORDER_THIN);
lineBreak.setBorderRight(HSSFCellStyle.BORDER_THIN);
lineBreak.setBorderTop(HSSFCellStyle.BORDER_THIN);
HSSFCellStyle lineBreak2 = wb.createCellStyle();
lineBreak2.setWrapText(true);// 設定自動換行
lineBreak2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 建立一個上下居中格式
lineBreak2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中// 設定邊框
lineBreak2.setBottomBorderColor(HSSFColor.BLACK.index);lineBreak2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
lineBreak2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
lineBreak2.setBorderRight(HSSFCellStyle.BORDER_THIN);
lineBreak2.setBorderTop(HSSFCellStyle.BORDER_THIN);
HSSFCell datacell = null;
for (int j = 0; j < columnNumber; j++) {
datacell = row.createCell(j);
datacell.setCellValue(dataList.get(i).get(j));
datacell.setCellStyle(lineBreak2);
}}
// 資料夾路徑
String fileth1 = "E://ExcelReport/" ;
//子資料夾名稱
String fileth2 =new Date().getTime()+"/";// 獲取資料夾
File file = new File(fileth1+fileth2+fileName+".xls");
//獲取上級資料夾
File parentFile = file.getParentFile();
if (!parentFile.exists() && !parentFile.isDirectory()) {
parentFile.mkdirs();
}
FileOutputStream fout = null;
try {
fout = new FileOutputStream(file);wb.write(fout);
} catch (Exception e) {
return null;} finally {
try {
fout.close();
} catch (IOException e) {
return null ;
}}
return 輸出地址 + fileth2+fileName;}
}
3呼叫
//資料格式化
List<List<String>> dataResource = new ArrayList<List<String>>();
//dataResource 加些資料
String sheetName = "報表";
String[] headers = { "學院", "課程數", "評分" };
String fileName = "學院報表";
int[] columnWidth = { 10, 10, 10 };
String result = exprot.generateExcel(sheetName, fileName, columnWidth,
fileName, headers, dataResource);
ResultData resultData = new ResultData();if (StringUtils.isBlank(result)) {
resultData.setMessage("匯出失敗");
} else {resultData.setMessage("匯出成功");
resultData.setData(result);
}
4結果
在我指定的位置生成了excel
程式碼簡單,樣式也醜,喜歡的可以自己去調