操作 POI 從資料庫匯出資料到excel表格
阿新 • • 發佈:2019-02-13
這裡先將要匯出的資料封裝到物件容器list中,得到users
if (users.size() > 0) {
// 建立Excel的工作書冊 Workbook,對應到一個excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 建立Excel的工作sheet,對應到一個excel文件的tab
HSSFSheet sheet = wb.createSheet("客戶列表");
// 設定excel每列寬度
sheet.setColumnWidth (0, 4000);
sheet.setColumnWidth(1, 4000);
sheet.setColumnWidth(2, 4000);
sheet.setColumnWidth(3, 4000);
sheet.setColumnWidth(4, 4000);
sheet.setColumnWidth(5, 4000);
sheet.setColumnWidth(6, 4000);
sheet.setColumnWidth(7, 4000);
sheet.setColumnWidth (8, 4000);
sheet.setColumnWidth(9, 4000);
sheet.setColumnWidth(10, 4000);
sheet.setColumnWidth(11, 4000);
sheet.setColumnWidth(12, 4000);
sheet.setColumnWidth(13, 4000);
sheet.setColumnWidth(14, 4000);
sheet.setColumnWidth(15, 4000);
// 建立字型樣式
HSSFFont font = wb.createFont ();
font.setFontName("Verdana");
font.setBoldweight((short) 100);
font.setFontHeight((short) 300);
font.setColor(HSSFColor.BLUE.index);
// 建立單元格樣式
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);
// 設定邊框
style.setBottomBorderColor(HSSFColor.RED.index);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setFont(font);// 設定字型
// 建立Excel的sheet的一行
HSSFRow row = sheet.createRow(0);
row.setHeight((short) 500);// 設定行的高度
// 建立一個Excel的單元格
HSSFCell cell = row.createCell(0);
// 合併單元格(startRow,endRow,startColumn,endColumn)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 11));
// 給Excel的單元格設定樣式和賦值
cell.setCellStyle(style);
String title = "使用者列表";
cell.setCellValue(title );
// 設定單元格內容格式時間
HSSFCellStyle style1 = wb.createCellStyle();
style1.setDataFormat(HSSFDataFormat.getBuiltinFormat("yyyy-mm-dd"));
style1.setWrapText(true);// 自動換行
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFCellStyle style2 = wb.createCellStyle();
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
row = sheet.createRow(1);// 建立Excel的sheet的資料標題行
cell = row.createCell(0);
cell.setCellStyle(style2);
cell.setCellValue("客戶編號");
cell = row.createCell(1);
cell.setCellStyle(style2);
cell.setCellValue("客戶名稱");
cell = row.createCell(2);
cell.setCellStyle(style2);
cell.setCellValue("客戶型別");
cell = row.createCell(3);
cell.setCellStyle(style2);
cell.setCellValue("聯絡人");
cell = row.createCell(4);
cell.setCellStyle(style2);
cell.setCellValue("聯絡電話");
cell = row.createCell(5);
cell.setCellStyle(style2);
cell.setCellValue("收貨時間");
cell = row.createCell(6);
cell.setCellStyle(style2);
cell.setCellValue("履約地點");
cell = row.createCell(7);
cell.setCellStyle(style2);
cell.setCellValue("地點名稱");
cell = row.createCell(8);
cell.setCellStyle(style2);
cell.setCellValue("對標門店價格");
cell = row.createCell(9);
cell.setCellStyle(style2);
cell.setCellValue("對標價格門店名稱");
cell = row.createCell(10);
cell.setCellStyle(style2);
cell.setCellValue("更新人");
cell = row.createCell(11);
cell.setCellStyle(style2);
cell.setCellValue("更新時間");
cell = row.createCell(12);
cell.setCellStyle(style2);
cell.setCellValue("建立時間");
cell = row.createCell(13);
cell.setCellStyle(style2);
cell.setCellValue("稽核狀態");
cell = row.createCell(14);
cell.setCellStyle(style2);
cell.setCellValue("稽核人員");
cell = row.createCell(15);
cell.setCellStyle(style2);
cell.setCellValue("稽核時間");
for (int j = 0; j <= users.size()-1 ; j++) {
User user = users.get(j);
String a = "未稽核";
if(user.getUserInfo().getCheck_status()!=null){
if(user.getUserInfo().getCheck_status()==0)
{ a = "未稽核";}
if(user.getUserInfo().getCheck_status()==1)
{ a = "稽核";}
}
int i = 0;
row = sheet.createRow(j+2);
cell = row.createCell(i);
cell.setCellStyle(style2);
cell.setCellValue(user.getUser_contract_no());
cell = row.createCell(++i);
cell.setCellStyle(style2);
cell.setCellValue(user.getUserInfo().getCust_name());
cell = row.createCell(++i);
cell.setCellStyle(style2);
cell.setCellValue(user.getUserInfo().getCust_group());
cell = row.createCell(++i);
cell.setCellStyle(style2);
cell.setCellValue(user.getUserInfo().getCust_con());
cell = row.createCell(++i);
cell.setCellStyle(style2);
cell.setCellValue(user.getUserInfo().getCon_tel());
cell = row.createCell(++i);
cell.setCellStyle(style2);
String json=user.getReceive_time();
StringBuilder builder=new StringBuilder();
if(StringUtils.isNotBlank(json)){
Map<String,String> map=Json.fromJson(Map.class,json);
for(Map.Entry<String,String> entity:map.entrySet()){
builder.append(entity.getKey()+param1+entity.getValue());
}
}
style2.setWrapText(true);//先設定為自動換行
cell.setCellStyle(style2);
cell.setCellValue(new HSSFRichTextString(builder.toString()));
cell = row.createCell(++i);
cell.setCellStyle(style2);
cell.setCellValue(user.getUserInfo().getContract_store_no());
cell = row.createCell(++i);
cell.setCellStyle(style2);
cell.setCellValue(user.getAddress());
cell = row.createCell(++i);
cell.setCellStyle(style2);
cell.setCellValue(user.getUserInfo().getTarget_store_no());
cell = row.createCell(++i);
cell.setCellStyle(style2);
cell.setCellValue(user.getUserInfo().getShop_name());
cell = row.createCell(++i);
cell.setCellStyle(style2);
cell.setCellValue(user.getUserInfo().getUpdate_user());
cell = row.createCell(++i);
cell.setCellStyle(style2);
cell.setCellValue(user.getUserInfo().getUpdate_time());
cell = row.createCell(++i);
cell.setCellStyle(style2);
cell.setCellValue(user.getUserInfo().getAddtime());
cell = row.createCell(++i);
cell.setCellStyle(style2);
cell.setCellValue(a);
cell = row.createCell(++i);
cell.setCellStyle(style2);
cell.setCellValue(user.getUserInfo().getCheck_user());
cell = row.createCell(++i);
cell.setCellStyle(style2);
cell.setCellValue(user.getUserInfo().getCheck_time());
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String excel_name = sdf.format(new Date());
try {
response.setContentType("application/x-download");
response.addHeader("Content-Disposition",
"attachment;filename=" + excel_name + ".xls");
OutputStream os = response.getOutputStream();
wb.write(os);
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}