資源簡介
ExcelUtils.java
代碼片段和文件信息
package?com.ansheng.util;
import?java.io.*;
import?java.lang.reflect.Field;
import?java.lang.reflect.Method;
import?java.net.URLEncoder;
import?java.text.SimpleDateFormat;
import?java.util.ArrayList;
import?java.util.Date;
import?java.util.List;
import?javax.servlet.http.HttpServletResponse;
import?org.apache.poi.hssf.usermodel.HSSFCell;
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.*;
import?org.apache.poi.xssf.usermodel.XSSFWorkbook;
public?class?ExcelUtils?{
????static?final?short?borderpx?=?1;
/**
?*?導(dǎo)出excel表格
?*?@param?head
?*?@param?body
?*?@return
?*/
????public?static?HSSFWorkbook?expExcel(List?head?List>?body)?{
????????HSSFWorkbook?workbook?=?new?HSSFWorkbook();
????????HSSFSheet?sheet?=?workbook.createSheet(“Sheet1“);
????????HSSFRow?row?=?sheet.createRow(0);
????????HSSFCell?cell=?null;
????????HSSFCellstyle?cellstyle?=?workbook.createCellstyle();
????????setBorderstyle(cellstyle?borderpx);
????????cellstyle.setFont(setFontstyle(workbook?“黑體“?(short)?14));
????????cellstyle.setAlignment(HSSFCellstyle.ALIGN_CENTER);
????????
????????for?(int?i?=?0;?i ????????????cell?=?row.createCell(i);
????????????cell.setCellValue(head.get(i));
????????????cell.setCellstyle(cellstyle);
????????}
????????
????????HSSFCellstyle?cellstyle2?=?workbook.createCellstyle();
????????setBorderstyle(cellstyle2?borderpx);
????????cellstyle2.setFont(setFontstyle(workbook?“宋體“?(short)?12));
????????cellstyle2.setAlignment(HSSFCellstyle.ALIGN_CENTER);
????????for?(int?i?=?0;?i?????????????row?=?sheet.createRow(i?+?1);
????????????List?paramList?=?body.get(i);
????????????for?(int?p?=?0;?p?????????????????cell?=?row.createCell(p);
????????????????cell.setCellValue(paramList.get(p));
????????????????cell.setCellstyle(cellstyle2);
????????????}
????????}
????????for?(int?i?=?0?isize?=?head.size();?i?????????????sheet.autoSizeColumn(i);?
????????}
????????return?workbook;
????}
????/**
?????*?文件輸出
?????*?@author?LiuYang
?????*?@param?workbook?填充好的workbook
?????*?@param?path?存放的位置
?????*/
????public?static?void?outFile(HSSFWorkbook?workbookString?pathHttpServletResponse?response)?{
????????SimpleDateFormat?fdate=new?SimpleDateFormat(“yyyyMMdd-HH點(diǎn)mm分“);
????????path?=?path.substring(0?path.lastIndexOf(“.“))?+?fdate.format(new?Date())?+?path.substring(path.lastIndexOf(“.“));
????????OutputStream?os=null;
????????File?file?=?null;
????????try?{
???????? file?=?new?File(path);
????????????String?filename?=?file.getName();
????????????os?=?new?FileOutputStream(file);
????????????response.addHeader(“Content-Disposition“?“attach
評論
共有 條評論