我有一个要求,我必须用多个HSSfSheets编写HSSfWorkbook。 对于第一张纸,我有不同的列,第二张有不同的列。对于每张表,我都写了不同的存储过程。 目前我的java代码是以只能处理一个存储过程数据的方式编写的。我想修改它并调用第二个存储过程来填充第二张表中的数据。我在ModelAndView对象中设置excel bean数据并构建文档。现在我的问题是如何获取每个模型并将其写入HSSfSheet?就像在控制器中一样,我在mapExcelBean和downloadExcelBean2中设置了两个bean。我想在第一张表中编写downloadExcelBean,在第二张表中编写downloadExcelBean2。请建议。 我的控制器功能如下。
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()");
}
在DownloadExcelView.java中,我构建了excel文档函数
var product = new Product();
procuct.Name = "Product1";
product.Items = new Dictionary<string, Item>();
product.Items.Add("Item1", new Item { Description="Lorem Ipsum" });