jxls에서 xls 모형을 내보냅니다.
3803 단어 JXLS
public class ExportExcelUtil {
private Map<String,Object> beans = new HashMap<String,Object>();
private String exportFileName = "export.xls";
public void setBeans(String key,Object obj){
//
beans.put(key, obj);
}
public ExportExcelUtil() {
// beans 。
//excel :${dateUtil.formatToDate(entity.checkDate)}
// ${dateUtil.formatToDateTime(entity.checkDateTime)}
beans.put("dateUtil", new DateUtil());
}
public void exportExcel(String fileName){
try {
Struts2Utils.getRequest().setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String systemPath = this.getClass().getResource("/").getPath();
//
String templateFileName = systemPath + "templates/"+fileName+".xls";
// File outFile = new File(outFileName);
XLSTransformer transformer = new XLSTransformer();
try {
InputStream is = new BufferedInputStream(new FileInputStream(templateFileName));
// transformer.transformXLS(templateFileName, beans, outFileName);
// ,
HSSFWorkbook workbook = (HSSFWorkbook) transformer.transformXLS(is, beans);
resetCellFormula(workbook);
// -
Struts2Utils.getResponse().reset();
// xls 2013-04-01
String outFileName = DateUtil.formatToDate(new Date())+new String(fileName.getBytes("gb2312"),"ISO8859-1");
Struts2Utils.getResponse().setHeader("Content-Type","application/force-download");
Struts2Utils.getResponse().setHeader("Content-Type","application/vnd.ms-excel");
Struts2Utils.getResponse().setHeader("Content-Disposition", "attachment;success=true;filename="+outFileName+".xls");
// response , excel
OutputStream os = Struts2Utils.getResponse().getOutputStream();
workbook.write(os);
is.close();
os.flush();
os.close();
} catch (ParsePropertyException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getExportFileName() {
return exportFileName;
}
public void setExportFileName(String exportFileName) {
this.exportFileName = exportFileName;
}
// 。
public void resetCellFormula(HSSFWorkbook wb) {
HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(wb);
//
int sheetNum = wb.getNumberOfSheets();
//
for (int i = 0; i < sheetNum; i++) {
HSSFSheet sheet = wb.getSheetAt(i);
int rows = sheet.getLastRowNum() + 1;//
for (int j = 0; j < rows; j++) {
HSSFRow row = sheet.getRow(j);
if (row == null)
continue;
int cols = row.getLastCellNum();//
for (int k = 0; k < cols; k++) {
HSSFCell cell = row.getCell(k);
if(cell!=null){
// ,
if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
cell.setCellFormula(cell.getCellFormula()); cell=e.evaluateInCell(cell);
}
} else {
continue;
}
}
}
}
}
프론트 데스크톱 js 호출.
window.location.href = url;
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JXLS 템 플 릿 에 따라 엑셀 인 스 턴 스 튜 토리 얼 내 보 내기본 논문 의 사례 는 JXLS 가 템 플 릿 에 따라 엑셀 인 스 턴 스 를 내 보 내 는 구체 적 인 방법 을 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다. 먼저 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.