pom
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
ExcelUtil.java
package com.common.util;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;
import org.apache.commons.lang.StringUtils;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ExcelUtil {
@SuppressWarnings("deprecation")
public static void WriteExcel(String templatePath, String title, int startRow, @SuppressWarnings("rawtypes") List dataList, OutputStream ops)
throws BiffException, IOException, RowsExceededException, WriteException {
WorkbookSettings settings = new WorkbookSettings();
settings.setWriteAccess(null);
Workbook wb = Workbook.getWorkbook(new File(templatePath));
ByteArrayOutputStream targetFile = new ByteArrayOutputStream();
WritableWorkbook wwb = Workbook.createWorkbook(targetFile, wb, settings);
WritableSheet wws = wwb.getSheet(0);
Label label = (Label) wws.getWritableCell(0, 0);
WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD);
WritableCellFormat cellFormat = new WritableCellFormat(font, NumberFormats.TEXT);
label.setString(title);
cellFormat.setWrap(true);
cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
for (int i = 0; i < dataList.size(); i++) {
Object[] objs = (Object[]) dataList.get(i);
for (int j = 0; j < objs.length; j++) {
char newLine = 0x000A;
if (objs[j] != null)
wws.addCell(new Label(j, i + startRow, String.valueOf(objs[j]).replace(';', newLine), cellFormat));
else
wws.addCell(new Label(j, i + startRow, "", cellFormat));
}
}
wwb.write();
wwb.close();
wb.close();
targetFile.writeTo(ops);
targetFile.close();
}
@SuppressWarnings("rawtypes")
public static Map ReadExcelToMap(final InputStream is) throws Exception {
return ReadExcelToMap(is, 0);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map ReadExcelToMap(final InputStream is, final int sheetNum) throws Exception {
Map returnMap = new HashMap();
Map rowMap = new HashMap();
int rowNum;
int colNum;
try {
Workbook wb = Workbook.getWorkbook(is);
Sheet sheet = wb.getSheet(sheetNum);
rowNum = sheet.getRows();
colNum = sheet.getColumns();
for (int i = 0; i < rowNum; i++) {
rowMap = new HashMap();
for (int j = 0; j < colNum; j++) {
rowMap.put(String.valueOf(j), sheet.getCell(j, i).getContents());
}
returnMap.put(String.valueOf(i), rowMap);
}
}
catch (BiffException biffe) {
biffe.printStackTrace();
throw new RuntimeException(biffe.toString());
}
return returnMap;
}
public static String encodingFileName(String fileName) {
String returnFileName = "";
try {
returnFileName = URLEncoder.encode(fileName, "UTF-8");
returnFileName = StringUtils.replace(returnFileName, "+", "%20");
if (returnFileName.length() > 150) {
returnFileName = new String(fileName.getBytes("GB2312"), "ISO8859-1");
returnFileName = StringUtils.replace(returnFileName, " ", "%20");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return returnFileName;
}
public static String getExcelTemplatePath(String fileDir) throws Exception {
String classDir = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
String fileBaseDir = classDir.substring(0, classDir.lastIndexOf("classes"));
if (!new File(fileBaseDir + fileDir).exists()) {
throw new Exception("模板文件不存在!");
}
return fileBaseDir + fileDir;
}
}
调用
response.setHeader("Content-Disposition", "attachment; filename=" + ExcelUtil.encodingFileName("信息_"+deviceSn+".xls"));
ExcelUtil.WriteExcel(ExcelUtil.getExcelTemplatePath("/excel_template/lack_detail.xls"), "信息 "+deviceSn, 2, listObj, response.getOutputStream());