第一種方式:代碼中自定義樣式(不推薦)
//獲取數據
List<OutProductVO> outProductList = this.outProductService.query(inputDate);
Workbook wb = new HSSFWorkbook();//創建一個工作簿
Sheet sheet = wb.createSheet("出貨表");//創建一個工作表
CellStyle cellStyle= wb.createCellStyle();
Font nFont = wb.createFont();
int rowNo = 0;//行號
int colNo = 1;//列號
Row nRow = null;//sheet.createRow(2);
Cell nCell = null;//nRow.createCell(1);
//設置大標題
sheet.addMergedRegion(new CellRangeAddress(0,0,1,9));
nRow = sheet.createRow(rowNo++);
nCell = nRow.createCell(colNo++);
nRow.setHeightInPoints(36);
nCell.setCellValue(inputDate.replace("-0", "年").replace("-","年")+"月份出貨表");
nCell.setCellStyle(bigTitleStyle(cellStyle, nFont));
//設置列寬
sheet.setColumnWidth(0, 2*272); //列寬
sheet.setColumnWidth(1, 26*272); //列寬 BUG,API底層設置不夠精確 256;272近似
sheet.setColumnWidth(2, 12*272);
sheet.setColumnWidth(3, 29*272);
sheet.setColumnWidth(4, 10*272);
sheet.setColumnWidth(5, 12*272);
sheet.setColumnWidth(6, 8*272);
sheet.setColumnWidth(7, 10*272);
sheet.setColumnWidth(8, 10*272);
sheet.setColumnWidth(9, 10*272);
//設置列標題
String[] title = new String[]{"客戶","訂單號","貨號","數量","工廠","附件","工廠交期","船期","貿易條款"};
nRow = sheet.createRow(rowNo++);
nCell=nRow.createCell(colNo++);
nRow.setHeightInPoints(26.25f);//設置行高
colNo=1;//列號置一
cellStyle= wb.createCellStyle();
nFont = wb.createFont();
for(int i=0;i<title.length;i++){
nCell=nRow.createCell(colNo++);
nCell.setCellValue(title[i]);
nCell.setCellStyle(titleStyle(cellStyle,nFont));
}
cellStyle= wb.createCellStyle();
nFont = wb.createFont();
for(int i=0;i<outProductList.size();i++){
colNo=1;
nRow = sheet.createRow(rowNo++);
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getCustomName());
nCell.setCellStyle(textStyle(cellStyle, nFont));
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getContractNo());
nCell.setCellStyle(textStyle(cellStyle, nFont));
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getContractNo());
nCell.setCellStyle(textStyle(cellStyle, nFont));
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getCnumber());
nCell.setCellStyle(textStyle(cellStyle, nFont));
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getFactoryName());
nCell.setCellStyle(textStyle(cellStyle, nFont));
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getExts());
nCell.setCellStyle(textStyle(cellStyle, nFont));
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getDeliveryPeriod());
nCell.setCellStyle(textStyle(cellStyle, nFont));
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getShipTime());
nCell.setCellStyle(textStyle(cellStyle, nFont));
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getTradeTerms());
nCell.setCellStyle(textStyle(cellStyle, nFont));
}
// FileOutputStream fileOut = new FileOutputStream("D://出貨表.xls");
//
// wb.write(fileOut);
//
// fileOut.close();
//下載
ByteArrayOutputStream bos = new ByteArrayOutputStream();
wb.write(bos);
DownloadUtil.download(bos, response, "出貨表.xls");;
bos.close();
第二種方式:採用模板方式,樣式在模板文件中通過手動方式設置好
//獲取打印數據
List<OutProductVO> outProductList = this.outProductService.query(inputDate);
//獲取打印模板路徑
String path =request.getSession().getServletContext().getRealPath("/");//服務器的根路徑
String filePath = path+"/make/xlsprint/tOUTPRODUCT.xls";
//獲取打印模板
Workbook wb = new HSSFWorkbook(new FileInputStream(new File(filePath)));
//獲取工作表
Sheet sheet = wb.getSheetAt(0);
Row row = null;
Cell cell =null;
row= sheet.getRow(0);
cell= row.getCell(1);
//設置大標題
cell.setCellValue(inputDate.replace("-0","年").replace("-","年")+"月份出貨表");
//獲取列樣式
row = sheet.getRow(2);
cell=row.getCell(1);
CellStyle customStyle = cell.getCellStyle();
row = sheet.getRow(2);
cell=row.getCell(2);
CellStyle contactNOStyle = cell.getCellStyle();
row = sheet.getRow(2);
cell=row.getCell(3);
CellStyle productNoStyle = cell.getCellStyle();
row = sheet.getRow(2);
cell=row.getCell(4);
CellStyle cnumberStyle = cell.getCellStyle();
row = sheet.getRow(2);
cell=row.getCell(5);
CellStyle factoryStyle = cell.getCellStyle();
row = sheet.getRow(2);
cell=row.getCell(6);
CellStyle extsStyle = cell.getCellStyle();
row = sheet.getRow(2);
cell=row.getCell(7);
CellStyle deliveryStyle = cell.getCellStyle();
row = sheet.getRow(2);
cell=row.getCell(8);
CellStyle shiptimeStyle = cell.getCellStyle();
row = sheet.getRow(2);
cell=row.getCell(9);
CellStyle tradetermsStyle = cell.getCellStyle();
//設置主題內容
int rowNo=2;
int colNo=1;
Row nRow=null;
Cell nCell=null;
for(int i=0;i<outProductList.size();i++){
colNo=1;
nRow = sheet.createRow(rowNo++);
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getCustomName());
nCell.setCellStyle(customStyle);
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getContractNo());
nCell.setCellStyle(contactNOStyle);
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getContractNo());
nCell.setCellStyle(productNoStyle);
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getCnumber());
nCell.setCellStyle(cnumberStyle);
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getFactoryName());
nCell.setCellStyle(factoryStyle);
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getExts());
nCell.setCellStyle(extsStyle);
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getDeliveryPeriod());
nCell.setCellStyle(deliveryStyle);
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getShipTime());
nCell.setCellStyle(shiptimeStyle);
nCell=nRow.createCell(colNo++);
nCell.setCellValue(outProductList.get(i).getTradeTerms());
nCell.setCellStyle(tradetermsStyle);
}
//下載文件
ByteArrayOutputStream bos = new ByteArrayOutputStream();
wb.write(bos);
DownloadUtil.download(bos, response, "出貨表.xls");
bos.close();