JAva poiエクスポート.xlsのExcelレポートIE 11エクスポートファイル名の接尾辞は.actionで、修正する
11102 ワード
//
public void exportStationWaterQualityData(){
List<Object> list=treeService.getExportStationWaterQualityData(stcd, startTm, endTm);
if (null!=list&&list.size()>0) {
List<Map<String,String>> factorList = (List<Map<String,String>>)list.get(0);//
List<Object[]> ItemList=(List<Object[]>)list.get(1);//
// Excel
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
response.reset();
try {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
HSSFCellStyle cellStyleMiddle = workbook.createCellStyle();
cellStyleMiddle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//
HSSFCellStyle cellStyleLeft = workbook.createCellStyle();
cellStyleLeft.setAlignment(HSSFCellStyle.ALIGN_CENTER);//
HSSFRow row;
HSSFCell cell;
//1、
row=sheet.createRow((int)0);
cell=row.createCell(0);
int sizeLen=factorList.size();
sheet.addMergedRegion(new CellRangeAddress(0, 0, (short)0, (short)sizeLen));
cell.setCellValue(reportTitle);//
cell.setCellStyle(cellStyleMiddle);//
//2、 ( )
row=sheet.createRow((int)1);
cell=row.createCell(0);
sheet.addMergedRegion(new CellRangeAddress(1, 1, (short)0, (short)sizeLen));
cell=row.createCell(0);
cell.setCellValue(" :。。。");//
cell.setCellStyle(cellStyleLeft);//
//3、
row=sheet.createRow(2);//
cell=row.createCell(0);
cell.setCellValue(" ");
cell.setCellStyle(cellStyleMiddle);//
//
for (int i = 0; i < factorList.size(); i++) {
cell=row.createCell(i+1);
if (null!=factorList.get(i).get("sampling_unit")&&!"".equals(factorList.get(i).get("sampling_unit"))) {
cell.setCellValue(factorList.get(i).get("name")+"("+factorList.get(i).get("sampling_unit")+")");
}else {
cell.setCellValue(factorList.get(i).get("name"));
}
cell.setCellStyle(cellStyleMiddle);//
}
//4、
Object[] arr;
for (int i = 0; i < ItemList.size(); i++) {
arr=ItemList.get(i);//
row=sheet.createRow(i+3);// , ......
for (int j = 0; j < arr.length; j++) {
cell=row.createCell(j);
cell.setCellValue(arr[j].toString());
cell.setCellStyle(cellStyleMiddle);//
}
}
//5、
response.setContentType("octets/stream");
String excelName = " ";
//
response.addHeader("Content-Disposition", "attachment;filename="+new String( excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");
OutputStream os = response.getOutputStream();
workbook.write(os);
os.close();
System.out.println("excel ");
} catch (Exception ioexception) {
request.setAttribute("message", "excel ");
}
}
}
, IE11 .action , :
List<Object> list=treeService.getExportStationWaterQualityData(stcd, startTm, endTm);
if (null!=list&&list.size()>0) {
List<Map<String,String>> factorList = (List<Map<String,String>>)list.get(0);//
List<Object[]> ItemList=(List<Object[]>)list.get(1);//
List<Object[]> ItemStyleList=(List<Object[]>)list.get(2);//
//
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
response.reset();
try {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
//title
HSSFCellStyle cellStyleMiddleTitle = workbook.createCellStyle();
cellStyleMiddleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 、
HSSFFont hssfFontTitle = workbook.createFont();
hssfFontTitle.setFontName(" ");
hssfFontTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//
hssfFontTitle.setFontHeightInPoints((short)10);//
cellStyleMiddleTitle.setFont(hssfFontTitle);
//remark
HSSFCellStyle cellStyleMiddleRemark = workbook.createCellStyle();
cellStyleMiddleRemark.setAlignment(HSSFCellStyle.ALIGN_CENTER);//
//
HSSFCellStyle cellStyleLeft = workbook.createCellStyle();
cellStyleLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT);//
HSSFRow row;
HSSFCell cell;
//1、
row=sheet.createRow((int)0);
cell=row.createCell(0);
int sizeLen=factorList.size();
sheet.addMergedRegion(new CellRangeAddress(0, 0, (short)0, (short)sizeLen));
cell.setCellValue(reportTitle);//
cell.setCellStyle(cellStyleMiddleTitle);//
//2、 ( )
row=sheet.createRow((int)1);
cell=row.createCell(0);
sheet.addMergedRegion(new CellRangeAddress(1, 1, (short)0, (short)sizeLen));
cell=row.createCell(0);
cell.setCellValue("( :。。。。。。)");//
cell.setCellStyle(cellStyleLeft);//
//3、
row=sheet.createRow(2);//
cell=row.createCell(0);
cell.setCellValue(" ");
cell.setCellStyle(cellStyleMiddleRemark);//
//
for (int i = 0; i < factorList.size(); i++) {
cell=row.createCell(i+1);
if (null!=factorList.get(i).get("sampling_unit")&&!"".equals(factorList.get(i).get("sampling_unit"))) {
cell.setCellValue(factorList.get(i).get("name")+"("+factorList.get(i).get("sampling_unit")+")");
}else {
cell.setCellValue(factorList.get(i).get("name"));
}
}
//4、
//
Object[] arr;
for (int i = 0; i < ItemList.size(); i++) {
arr=ItemList.get(i);//
row=sheet.createRow(i+3);// , ......
for (int j = 0; j < arr.length; j++) {
cell=row.createCell(j);
cell.setCellValue(arr[j].toString());
}
}
//
//
HSSFCellStyle cellStyle_Green = workbook.createCellStyle();
HSSFFont hssfFont_Green = workbook.createFont();
hssfFont_Green.setColor(HSSFColor.GREEN.index);
cellStyle_Green.setFont(hssfFont_Green);
cellStyle_Green.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//
HSSFCellStyle cellStyle_Blue = workbook.createCellStyle();
HSSFFont hssfFont_Blue = workbook.createFont();
hssfFont_Blue.setColor(HSSFColor.BLUE.index);
cellStyle_Blue.setFont(hssfFont_Blue);
cellStyle_Blue.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//
HSSFCellStyle cellStyle_Red = workbook.createCellStyle();
HSSFFont hssfFont_Red = workbook.createFont();
hssfFont_Red.setColor(HSSFColor.RED.index);
cellStyle_Red.setFont(hssfFont_Red);
cellStyle_Red.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//
HSSFCellStyle cellStyle_Black = workbook.createCellStyle();
HSSFFont hssfFont_Black = workbook.createFont();
hssfFont_Black.setColor(HSSFColor.BLACK.index);
cellStyle_Black.setFont(hssfFont_Black);
cellStyle_Black.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//
Object[] obj;
for (int i = 0; i < ItemStyleList.size(); i++) {
obj=ItemStyleList.get(i);//obj=[green;0;5, green;0;6, green;0;7, green;0;8, blue;0;12]= - -
// System.out.println(" obj:"+obj[0].toString());
if (null!=obj&&obj.length>0) {
for (int j = 0; j < obj.length; j++) {
String styleObjStr=(String)obj[j];
String[] arra=styleObjStr.split(";");
row=sheet.getRow(Integer.parseInt(arra[1])+3);//
cell=row.getCell(Integer.parseInt(arra[2]));//
if ("green".equals(arra[0])) {
cell.setCellStyle(cellStyle_Green);//
} else if ("blue".equals(arra[0])) {
cell.setCellStyle(cellStyle_Blue);//
} else if ("red".equals(arra[0])) {
cell.setCellStyle(cellStyle_Red);//
}else {
cell.setCellStyle(cellStyle_Black);//
}
}
} else {
//do nothing
}
}
//5、
response.setContentType("octets/stream");
String excelName = "。。。。 ";
//
response.addHeader("Content-Disposition", "attachment;filename="+new String( excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");
OutputStream os = response.getOutputStream();
workbook.write(os);
os.close();
System.out.println("excel ");
} catch (Exception ioexception) {
request.setAttribute("message", "excel ");
}
}
, :
/*
response.setContentType("octets/stream");
String excelName = "。。。 ";
response.addHeader("Content-Disposition", "attachment;filename="+new String( excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");
OutputStream os = response.getOutputStream();
workbook.write(os);
os.flush();
os.close();
*/
String excelName = "。。。 ";
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" +new String( excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");
OutputStream ouputStream = response.getOutputStream();
workbook.write(ouputStream);
ouputStream.flush();
ouputStream.close();