java 导出excel 例子_java导出Excel例子
//在service層寫的,在action直接調用此方法就行了
//導出Excel
public boolean exportExcel(HttpServletResponse response,List list)
{
try
{
OutputStream os = response.getOutputStream();// 取得輸出流
response.reset();// 清空輸出流
response.setHeader("Content-disposition", "attachment; 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{
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;
}
}
總結
以上是生活随笔為你收集整理的java 导出excel 例子_java导出Excel例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中sys模块有什么用_Pyt
- 下一篇: 昆明大学津桥学院计算机科学与技术,昆明理