SummaryInformation 常用 Getter:getTitle()、getAuthor()、getCreateDateTime()、getCharCount()、getApplicationName() 等。DocumentSummaryInformation:getCategory()、getMultimediaClipCount() 等。PropertySet 通用 API 遍歷 Property 數組,較複雜,參考官方 HOW-TO。public class HSSFCreate extends HttpServlet {
protected void processRequest(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("application/vnd.ms-excel");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short)0);
row.createCell((short)0).setCellValue(1);
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("字符串值");
row.createCell((short)3).setCellValue(true);
OutputStream out = resp.getOutputStream();
wb.write(out);
out.close();
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
processRequest(req, resp);
}
public String getServletInfo() { return "Servlet 動態生成 Excel 示例"; }
}
web.xml 映射 Servlet 路徑(如 /export.xls)。SXSSFWorkbook (POI 3.8+) 流式寫入,避免 OOM。三篇文章涵蓋:Usermodel 易用讀寫、Eventusermodel 高性能流式讀取、HPSF 文檔屬性讀取、Servlet 動態輸出。POI 功能強大,更多特性(圖表、樞紐分析、XML 轉 Excel)待開發者深入探索。