发布网友 发布时间:2022-04-25 22:05
共1个回答
热心网友 时间:2023-10-15 20:35
嗨 你好
你可以参考一下:
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
/**
* 导出excel
* @param reportParams 导出excel列名标示
* @param list导出excel值
* @param headersexcel头
* @param reportName·excel的sheet名
* @param response
* @param startRow从哪一行开始放list值
* @param startCol 从哪一列开始放list值
* @return
*/
public boolean report(ReportBean rb, List list, XlsHeaderBean[] headers, String reportName, HttpServletResponse response, int startRow, int startCol, HttpServletRequest request){
WritableWorkbook wwb = null;
OutputStream os;
boolean flag = true;
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String strDate = format.format(date);
try {
// 用Workbook类的工厂方法创建工作薄(Workbook)对象
response.setContentType("application/x-msdownload");
String sheetName = "report";
sheetName = sheetName.replaceAll(":", "").replaceAll("[)]", "")
.replaceAll("[(]", "");
// 这里解释一下
// attachment; 这个代表要下载的,如果去掉就编程直接打开了
// filename是文件名,另存为或者下载时,为默认的文件名
response.addHeader("Content-Disposition", "attachment; filename="
+ new String(sheetName.getBytes("UTF-8"), "ISO-8859-1")+ strDate
+ ".xls");
os = response.getOutputStream();
wwb = Workbook.createWorkbook(os);
} catch (IOException e) {
e.printStackTrace();
return flag = false;
}
if (wwb != null) {
// 创建一个可写入的工作表
// Workbook的createSheet方法两个参数,1名称,2位置
WritableSheet ws = wwb.createSheet(reportName, 0);
// 下面开始添加单元格
// 导出excel
try {
Label labelC = null;
for(int j = 0; j < headers.length; j++){
if(headers[j].isUnion()){
ws.mergeCells(headers[j].getCol(), headers[j].getRow(), headers[j].getCol()+headers[j].getColLength(), headers[j].getRow()+headers[j].getRowLength());
}
labelC = new Label(headers[j].getCol(), headers[j].getRow(), headers[j].getValue());
ws.addCell(labelC);
}
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
String value = null;
for (int i = 0; i < list.size(); i++) {
Map values = (Map)list.get(i);
for (int j = 0; j < rb.getReportParams().length; j++) {
// 这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行
Label labelC;
if(values.get(rb.getReportParams()[j]) == null){
value = "";
} else {
if(values.get(rb.getReportParams()[j]) instanceof java.util.Date){
value = format.format(values.get(rb.getReportParams()[j]));
} else if(values.get(rb.getReportParams()[j]) instanceof java.math.BigDecimal){
value = values.get(rb.getReportParams()[j]).toString();
} else {
value = values.get(rb.getReportParams()[j]).toString();
}
}
labelC = new Label(j+startCol, i + startRow, value);
try {
// 将生成的单元格添加到工作表中
ws.addCell(labelC);
} catch (RowsExceededException e) {
e.printStackTrace();
return flag = false;
} catch (WriteException e) {
e.printStackTrace();
return flag = false;
}
}
}
if(rb.isHasImg()){
String rootPath = this.getServletContext().getRealPath("savefiles");
String imgPath = rb.getImgPath();
if(rootPath != null && rootPath.compareTo("") != 0 && imgPath != null && imgPath.compareTo("") !=0){
String[] strs = imgPath.split("\\/");
String imgName = strs[strs.length-1];
File file = new File(rootPath + File.separator + imgName);
if(file.exists()){
WritableImage wi = new WritableImage(0, startRow + list.size() + 2,12,20, file);
ws.addImage(wi);
}
}
}
try {
// 从内存中写入文件中
wwb.write();
wwb.close();
return flag;
} catch (IOException e) {
e.printStackTrace();
return flag = false;
} catch (WriteException e) {
e.printStackTrace();
return flag = false;
}
}
return flag;
}