JAVA后端代码
package ext.corilead.report;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import wt.fc.Persistable;
import wt.fc.ReferenceFactory;
import wt.fc.WTReference;
import wt.method.RemoteAccess;
import wt.part.WTPart;
import wt.util.WTException;
import wt.util.WTProperties;
public class ExportPartDetailReports implements RemoteAccess, Serializable {
private static String wtHome = "";
private static WTProperties wtProperties;
static {
try {
wtProperties = WTProperties.getLocalProperties();
wtHome = wtProperties.getProperty("wt.home");
} catch (IOException e) {
e.getMessage();
}
}
public static XSSFWorkbook buildExcel (String oid) throws WTException, IOException {
String[] tytle = {"部件编号", "部件名称", "生命周期", "库名称", "创建者"};
WTReference partRef = new ReferenceFactory().getReference(oid);
WTPart part = (WTPart) partRef.getObject();
String filePath = getTemplate();
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(filePath));
XSSFSheet sheet = wb.createSheet("sheet1");
XSSFRow row = sheet.createRow(0);
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
XSSFCell cell = null;
String[] values = {part.getNumber(), part.getName(), part.getLifeCycleState().getDisplay(),
part.getContainer().getName(), part.getCreatorFullName()};
for (int i = 0; i < tytle.length; i++) {
cell = row.createCell(i);
cell.setCellValue(tytle[i]);
cell.setCellStyle(cellStyle);
}
row = sheet.createRow(1);
for (int i = 0; i < tytle.length; i++) {
row.createCell(i).setCellValue(values[i]);
}
return wb;
}
private static String getTemplate() {
String filePath;
filePath = wtHome + File.separator + "codebase" + File.separator + "templates" + File.separator + "导出的表.xlsx";
return filePath;
}
}
jsp代码
<%@ page contentType="text/html; charset=UTF-8" language="java"
import="java.util.*,
wt.part.WTPart,ext.corilead.report.*,wt.fc.*,java.io.*,org.apache.poi.hssf.usermodel.*"
pageEncoding="UTF-8" autoFlush="true" buffer="3000kb"%>
<%@ page import="org.apache.poi.xssf.usermodel.XSSFWorkbook" %>
<%@include file="/netmarkets/jsp/components/beginWizard.jspf"%>
<%
String oid =request.getParameter("oid");
XSSFWorkbook wb = ExportPartDetailReports.buildExcel(oid);
String fileName = "导出的表.xlsx";
fileName =new String(fileName.getBytes(),"ISO-8859-1");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.addHeader("Content-Disposition", "attachment;filename="+ fileName );
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
try {
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
<%@include file="/netmarkets/jsp/util/end.jspf"%>