java 匯出到EXCEL
package org.fh.util; import java.util.Date; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.Workbook; import org.fh.entity.PageData; import org.springframework.web.servlet.view.document.AbstractXlsView; /** * 說明:匯出到EXCEL * 作者:FH Admin * from:fhadmin.cn */ public class ObjectExcelView extends AbstractXlsView{ @Override protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub Date date = new Date(); String filename = DateUtil.date2Str(date, "yyyyMMddHHmmss"); HSSFSheet sheet; response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename="+filename+".xls"); HSSFWorkbook book = (HSSFWorkbook) workbook; sheet = book.createSheet("sheet1"); List<String> titles = (List<String>) model.get("titles"); int len = titles.size(); HSSFCellStyle headerStyle = book.createCellStyle(); //標題樣式 headerStyle.setAlignment(HorizontalAlignment.CENTER); headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); HSSFFont headerFont = book.createFont(); //標題字型 headerFont.setBold(true); headerFont.setFontHeightInPoints((short)11); headerStyle.setFont(headerFont); short height=25*20; HSSFRow row = sheet.createRow(0); for(int i=0; i<len; i++){ //設定標題 String title = titles.get(i); row.setRowStyle(headerStyle); row.createCell(i).setCellValue(title); } sheet.getRow(0).setHeight(height); HSSFCellStyle contentStyle = book.createCellStyle(); //內容樣式 contentStyle.setAlignment(HorizontalAlignment.CENTER); List<PageData> varList = (List<PageData>) model.get("varList"); int varCount = varList.size(); for(int i=0; i<varCount; i++){ PageData vpd = varList.get(i); HSSFRow rows = sheet.createRow(i+1); for(int j=0;j<len;j++){ String varstr = vpd.getString("var"+(j+1)) != null ? vpd.getString("var"+(j+1)) : ""; rows.setRowStyle(contentStyle); rows.createCell(j).setCellValue(varstr); } } } }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31558068/viewspace-2851923/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- hive匯出到csv hive匯出到excelHiveExcel
- 匯出到Excel的操作Excel
- 將資料匯出到ExcelExcel
- json陣列匯出到ExcelJSON陣列Excel
- 表結構匯出到excel中Excel
- fastadmin的匯出到excel功能ASTExcel
- Vue+element ui table 匯出到excelVueUIExcel
- 將MYSQL中資料匯出到EXCEL表MySqlExcel
- C# 從datagridview中匯出到excelC#ViewExcel
- DataGrid 匯出到 Excel 的幫助類Excel
- 將dataGridView內容匯出到Excel檔案ViewExcel
- ORACLE資料匯出到Excel、txt、HTML實用方法OracleExcelHTML
- 用SQL Loader將Excel資料匯出到OracleSQLExcelOracle
- Java匯出ExcelJavaExcel
- Java 通過Xml匯出Excel檔案,Java Excel 匯出工具類,Java匯出Excel工具類JavaXMLExcel
- LOTUS/DOMINO學習筆記之匯出到EXCEL的方法筆記Excel
- Excel匯入匯出神器(Java)ExcelJava
- 多個報表匯出到一個 excel 的多 sheet 頁Excel
- ASP.NET 2.0匯出到Excel時保留換行的方法ASP.NETExcel
- C# asp.net 把GridView資料 匯出到 ExcelC#ASP.NETViewExcel
- 第四節:海量資料匯出到Excel、百萬Excel匯入到DB、秒級/分鐘級排行版Excel
- 轉java操作excel匯入匯出JavaExcel
- Dynamics CRM 資料匯出到Excel時列標題不能重複Excel
- java匯出Excel檔案JavaExcel
- java 匯入到EXCEL表格JavaExcel
- java poi 匯出excel加密JavaExcel加密
- java匯出Excel定義匯出模板JavaExcel
- java讀取Excel匯入匯出工具JavaExcel
- java 匯入匯出Excel工具類ExcelUtilJavaExcel
- Java之POI操作Excel表-匯入匯出JavaExcel
- Java進行excel的匯入匯出操作JavaExcel
- 關於java中Excel的匯入匯出JavaExcel
- Java POI匯入Excel檔案JavaExcel
- poi 匯出Excel java程式碼ExcelJava
- 轉載:利用SQL*Loader將 Excel 資料匯出到資料庫中SQLExcel資料庫
- java使使用者EasyExcel匯入匯出excelJavaExcel
- MSSQL資料匯出到MYSQLMySql
- 用ExportToXls方法匯出到Excel就變成這樣一團的解決方法ExportExcel