我有一个要求,我必须用多个 HSSfSheets 编写 HSSfWorkbook。对于第一张表,我有不同的列,第二张表有不同的列集。对于每张工作表,我编写了不同的存储过程。目前我的java代码是以一种只能处理一个存储过程数据的方式编写的。我想修改它并调用第二个存储过程来填充第二张表中的数据。我在 ModelAndView 对象中设置 excel bean 数据并构建文档。现在我的问题是如何获取每个模型并将其写入 HSSfSheet?就像在控制器中一样,我在地图 downloadExcelBean 和 downloadExcelBean2 中设置了两个 bean。我想在第一张纸上写 downloadExcelBean 并在第二张纸上下载 ExcelBean2 。请建议。我的控制器功能如下。
public ModelAndView exportToExcel(String fileName1, String filename2,String sheetname1,String sheetName2,
String[] headerLabels1, String[] headerLabels2,int[] datatypeArray1,int[] datatypeArray2, String procString1,String procString2,
Object[] objarray, HttpServletRequest arg0,
HttpServletResponse arg1, String[] dbColumnNameArray1,String[] dbColumnNameArray2)
throws Exception {
Log.info(DownloadExcelController.class,
"Execution Starts..exportExcel()");
Map<String, DownloadExcelBean> downloadExcelBean = new HashMap<String, DownloadExcelBean>();
//System.out.println(" in export excel part....."+procString+":::"+datatypeArray);
List<List> workLists1 = downloadExcelService.storedProcedureToExcel(
datatypeArray1, procString1, objarray, dbColumnNameArray1);
List<List> workLists2 = downloadExcelService.storedProcedureToExcel(
datatypeArray2, procString2, objarray, dbColumnNameArray2);
DownloadExcelBean bean = new DownloadExcelBean();
bean.setFilename(fileName1);
bean.setSheetname(sheetname1);
bean.setHeaderArray(headerLabels1);
bean.setDatatypeArray(datatypeArray1);
// bean.setVisibleArray(visibleArray);
bean.setData(workLists1);
DownloadExcelBean bean2 = new DownloadExcelBean();
bean2.setFilename(filename2);
bean2.setSheetname(sheetName2);
bean2.setHeaderArray(headerLabels2);
bean2.setDatatypeArray(datatypeArray2);
bean2.setData(workLists2);
downloadExcelBean.put("downloadExcelBean", bean);
downloadExcelBean.put("downloadExcelBean2", bean2);
return new ModelAndView("DownloadExcelView", "model",
downloadExcelBean);
}
在 DownloadExcelView.java 我建立了 excel 文档功能
protected void buildExcelDocument(Map model, HSSFWorkbook workbook,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Log.info(DownloadExcelView.class,
"Execution starts....buildExcelDocument()");
response.setHeader("Content-Type", "application/vnd.ms-excel");
Map<String, DownloadExcelBean> exceldownload = (Map<String, DownloadExcelBean>) model.get("model");
for (Map.Entry<String, DownloadExcelBean> mapobject : exceldownload.entrySet())
{
DownloadExcelBean obj = (DownloadExcelBean) mapobject.getValue();
String filename = obj.getFilename(); // File name
String sheetname = obj.getSheetname(); // sheet Name
String headerlist[] = obj.getHeaderArray(); // header names
int dataTypeArray[] = obj.getDatatypeArray();
List resultsetValues = obj.getData();
response.setHeader("Content-Disposition", "attachment; filename="
+ filename + ".xls");
HSSFSheet sheet = workbook.createSheet(sheetname); // create a sheet
HSSFSheet sheet1 = workbook.createSheet("Second tab");
HSSFCellStyle cellStyle = setHeaderStyle(workbook); // Apply bold
// for header
HSSFRow header = sheet1.createRow(0); // create a header row
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
short decimalDataformat = workbook.createDataFormat().getFormat(
"#,###,##0.00");
HSSFCellStyle decimalCellStyle = workbook.createCellStyle();
decimalCellStyle.setDataFormat(decimalDataformat);
HSSFCellStyle wrapStyle = workbook.createCellStyle();
wrapStyle.setWrapText(true);
int visibleCol = -1;
for (int i = 0; i < headerlist.length; i++) {
visibleCol++;
HSSFCell cell = header.createCell(visibleCol);
cell.setCellStyle(cellStyle);
cell.setCellValue(headerlist[i].replace("<br/>", ARConstants.EXPORT_CRLF));
}
// Excel cell values
HSSFRow hssfrow;
HSSFCell cell;
for (int row = 0; row < resultsetValues.size(); row++) {
List rowList = (ArrayList) resultsetValues.get(row);
hssfrow = sheet1.createRow(row + 1);
visibleCol = -1;
for (int col = 0; col < rowList.size(); col++) {
// if(visibleArray[col]){
visibleCol++;
cell = hssfrow.createCell(visibleCol);
if (dataTypeArray[col] == java.sql.Types.DECIMAL) {
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellStyle(decimalCellStyle);
if (rowList.get(col) != null)
cell.setCellValue((Double) rowList.get(col));
else
cell.setCellValue("");
} else if (dataTypeArray[col] == java.sql.Types.DATE) {
if (rowList.get(col) != null)
cell.setCellValue(dateFormat.format(rowList
.get(col)));
else
cell.setCellValue("");
}
else if (dataTypeArray[col] == java.sql.Types.TIMESTAMP) {
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(wrapStyle);
if (rowList.get(col) != null)
cell.setCellValue(StringUtils.dateToUSFormatDateTimeTillMins(rowList.get(col).toString())
.replace(" ", ARConstants.EXPORT_CRLF));
else
cell.setCellValue("");
}
else if (dataTypeArray[col] == java.sql.Types.OTHER)
{
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellStyle(wrapStyle);
String crlf = Character.toString((char) 13)
+ Character.toString((char) 10);
if (rowList.get(col) != null)
{
String str=(rowList.get(col).toString()).replaceAll("\t"," ");
if((str.replace("|", crlf)).length()> 32767)
cell.setCellValue((str.replace("|", crlf)).substring(0,32766));
else
cell.setCellValue(str.replace("|", crlf));
}
else
cell.setCellValue("");
}
else
{
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
if (rowList.get(col) != null)
{
String str=(rowList.get(col).toString()).replaceAll("\t"," ");
if(str.length() > 32767)
{
cell.setCellValue(str.substring(0,32766));
}
else
{
cell.setCellValue(str);
}
}
else
cell.setCellValue("");
}
// sheet.autoSizeColumn(col);
}
}
}
Log.info(DownloadExcelView.class,
"Execution ends....buildExcelDocument()");
}