public boolean exportExcel(HttpServletResponse response,List<Cityinfo> list)
{
try
{
OutputStream os = response.getOutputStream();// 取得輸出流
response.reset();// 清空輸出流
response.setHeader("Content-disposition", "p_w_upload; filename=fine.xls");// 設定輸出文件頭
response.setContentType("application/msexcel");// 定義輸出類型
WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件
String tmptitle = "財務報表"; // 標題
WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名稱
// 設置excel標題
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,WritableFont.BOLD,
false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
WritableCellFormat wcfFC = new WritableCellFormat(wfont);
wcfFC.setBackground(Colour.AQUA);
wsheet.addCell(new Label(1, 0, tmptitle, wcfFC));
wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14,WritableFont.BOLD,
false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
wcfFC = new WritableCellFormat(wfont);
// 開始生成主體內容
wsheet.addCell(new Label(0, 2, "城市代碼"));
wsheet.addCell(new Label(1, 2, "城市名"));
for(int i=0;i<list.size();i++)
{
wsheet.addCell(new Label(0, i+3, list.get(i).getCityid())); //數據庫的城市代碼字段
wsheet.addCell(new Label(1, i+3, list.get(i).getName())); //數據庫的城市名字段
}
// 主體內容生成結束
wbook.write(); // 寫入文件
wbook.close();
os.close(); // 關閉流
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}