1.以下是我封装的Java类
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
*
* @author HeYunLai
*
*/
public class ClubMissLiCodeExportUtil {
public static void exportByTemplate(HttpServletRequest request,HttpServletResponse response,List<List<String>> dataList,String tmpPath,int startRowNum,int sheetNum,String downloadName) throws Exception {
XSSFWorkbook workbook = getWorkbookTmp(request,tmpPath);
XSSFSheet sheet = workbook.getSheetAt(sheetNum);
setDataIntoSheet(sheet,dataList,startRowNum);
XSSFCellStyle style = workbook.createCellStyle();
setBorderStyle(style);
setSheetStyle(sheet,style,startRowNum,(short) 500);
response.setHeader("Content-Disposition", "attachment; filename=\""
+ new String((downloadName + ".xlsx").getBytes("GBK"),
"iso8859-1")
+ "\"");
response.setHeader("Content-Type", "application/octet-stream");
OutputStream out = response.getOutputStream();
workbook.write(out);
out.close();
return ;
}
/**
* 获得模板
* @param request
* @param tmpPath 例如:/test/a.xlsx
* @return
* @throws Exception
*/
public static XSSFWorkbook getWorkbookTmp(HttpServletRequest request,String tmpPath) throws Exception {
String filePath = request.getSession().getServletContext().getRealPath(tmpPath);
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(filePath));
return workbook;
}
/**
* 设置样式,有边框、居中
* @param style
*/
public static void setBorderStyle(XSSFCellStyle style) {
style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
style.setBorderTop(XSSFCellStyle.BORDER_THIN);
style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
style.setBorderRight(XSSFCellStyle.BORDER_THIN);
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
}
/**
* 往sheet写入数据
* @param sheet
* @param dataList 数据
* @param startRowNum 起始行
*/
public static void setDataIntoSheet(XSSFSheet sheet, List<List<String>> dataList, int startRowNum) {
int rowNum = startRowNum;
XSSFRow row = null;
XSSFCell cell = null;
for (List<String> rowData : dataList) {
row = sheet.createRow(rowNum);
for (int i = 0; i < rowData.size(); i++) {
cell = row.createCell(i);
cell.setCellValue(rowData.get(i));
}
rowNum++;
}
}
/**
* 设置sheet样式
* @param workbook
* @param style 每个一格样式
* @param sheetNum sheet位置
* @param startRowNum 起始行
* @param rowHeight 行高,建议500
*/
public static void setSheetStyle(XSSFSheet sheet, XSSFCellStyle style, int startRowNum,short rowHeight) {
int rowNum = startRowNum;
XSSFRow row = null;
XSSFCell cell = null;
for (int i = rowNum; i < sheet.getLastRowNum()+1; i++) {
row = sheet.getRow(i);
row.setHeight(rowHeight);
for (int j = 0; j < row.getLastCellNum(); j++) {
cell = row.getCell(j);
cell.setCellStyle(style);
}
rowNum++;
}
}
}
使用示例
private void test(HttpServletRequest request,HttpServletResponse response) throws Exception {
List<List<String>> dataList = new ArrayList<>();
for(int i=0;i<4;i++){
List<String> data = new ArrayList<>();
data.add(""+i);
data.add(""+i);
data.add(""+i);
data.add(""+i);
dataList.add(data);
}
String tmpPath = "/cmcc/supplier/common/ping.xlsx";
int startRowNum = 2;
int sheetNum = 0;
String downloadName = "hhh";
ClubMissLiCodeExportUtil.exportByTemplate(request, response, dataList, tmpPath, startRowNum, sheetNum, downloadName);
return ;
}