瀏覽器根據 Content-Type 決定如何處理響應:內嵌顯示、觸發下載、或啟動關聯應用程序。
| 格式 | Content-Type | 副檔名 | Content-Disposition 範例 |
|---|---|---|---|
| Word | application/msword | .doc | attachment; filename="report.doc" |
| Word (OOXML) | application/vnd.openxmlformats-officedocument.wordprocessingml.document | .docx | attachment; filename="report.docx" |
| Excel | application/vnd.ms-excel | .xls | attachment; filename="data.xls" |
| Excel (OOXML) | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet | .xlsx | attachment; filename="data.xlsx" |
| application/pdf | inline; filename="doc.pdf" (預覽) / attachment (下載) |
<%@ page contentType="application/msword;charset=UTF-8" %>
<% response.setHeader("Content-Disposition", "attachment; filename=export.doc"); %>
<html><body>
<h1><%= title %></h1>
<table border="1"><tr><th>欄位1</th><th>欄位2</th></tr>
<% for (Item item : list) { %>
<tr><td><%= item.getCol1() %></td><td><%= item.getCol2() %></td></tr>
<% } %>
</table>
</body></html>
缺點:Office 會嘗試解析 HTML,複雜樣式/圖片/分頁失效,兼容性差。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.3.0</version>
</dependency>
<%@ page import="org.apache.poi.xssf.usermodel.*, org.apache.poi.ss.usermodel.*, java.io.*, java.util.*" %>
<%
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("報表.xlsx", "UTF-8"));
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("數據");
Row header = sheet.createRow(0);
String[] cols = {"ID", "姓名", "金額", "日期"};
for (int i = 0; i < cols.length; i++) header.createCell(i).setCellValue(cols[i]);
// 假設從資料庫查詢
List
XWPFDocument doc = new XWPFDocument();
doc.createParagraph().createRun().setText("標題: " + title);
XWPFTable table = doc.createTable(data.size()+1, cols.length);
// 填充表頭與數據...
doc.write(response.getOutputStream());
doc.close();
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.36</version>
</dependency>
<%@ page import="com.lowagie.text.*, com.lowagie.text.pdf.*, java.io.*, java.awt.Color" %>
<%
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode("報表.pdf", "UTF-8"));
Document document = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
// 中文字體(需註冊 TTF)
BaseFont bfChinese = BaseFont.createFont("c:/windows/fonts/msjh.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bfChinese, 12);
document.add(new Paragraph("銷售報表", new Font(bfChinese, 18, Font.BOLD)));
document.add(new Paragraph("生成時間: " + new java.util.Date(), font));
PdfPTable table = new PdfPTable(4);
table.setWidths(new float[]{1, 3, 2, 2});
for (String h : new String[]{"編號", "商品", "單價", "數量"}) {
table.addCell(new PdfPCell(new Phrase(h, font)).setBackgroundColor(Color.LIGHT_GRAY));
}
for (Object[] row : getDataFromDB()) {
for (Object cell : row) table.addCell(new Phrase(String.valueOf(cell), font));
}
document.add(table);
document.close();
%>
前端生成 HTML → 後端調用 LibreOffice Headless / 商業轉換服務 → 返回 PDF/Word → 下載。適合複雜排版、印章、簽章需求。
| 場景 | 推薦方案 |
|---|---|
| 簡單表格導出、內部工具 | POI (XLSX) / 簡單 HTML 偽裝 |
| 複雜報表、樣式、圖表、分頁 | POI + 樣式 API / 模板填充 |
| 正式合同、發票、需精準排版 | OpenPDF / iText / 商業轉換服務 |
| 大數據量導出(>10萬行) | POI SXSSF (流式) / CSV / 分批次生成 ZIP |
URLEncoder.encode(filename, "UTF-8") 處理中文response.setBufferSize(65536) 減少 flush 次數