import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.Region;
public class excelUtil {
/**
* excel 导出
* @param excelName 文件名称
* @param title 表名称
* @param dataList 表格数据
* @param titleList 表头名称
* @param header 表头的map键值
* @param response
*/
@SuppressWarnings("deprecation")
public static void exportExcelWithTitle(String excelName, String title,
List<Map<String, Object>> dataList, List<String> titleList,
List<String> header,HttpServletResponse response){
OutputStream out = null;
try {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
for (int i = 0; i < titleList.size(); i++)
{
sheet.setColumnWidth(i, String.valueOf(titleList.get(i)).getBytes().length * 2 * 256);
}
HSSFRow row = null;
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment((short)2);
row = sheet.createRow(0);
sheet.addMergedRegion(new Region(0, (short)0, 0, (short)(header.size()-1)));
HSSFCell cell1 = row.createCell((short)0);
cell1.setCellStyle(style);
cell1.setCellValue(title);
row = sheet.createRow(1);
createTitle(row, titleList);
Map<String, Object> dMap = null;
for (int i = 0; i < dataList.size(); i++) {
dMap = (Map)dataList.get(i);
row = sheet.createRow(i + 2);
HSSFCell cell = null;
int col = 0;
for (int j = 0; j < header.size(); j++) {
cell = row.createCell(col++);
cell.setCellValue(
dMap.get(header.get(j)) == null ? "--" : dMap.get(header.get(j)).toString());
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
byte[] data = baos.toByteArray();
if (data != null) {
response.reset();
response.setContentType("application/x-download;charset=UTF-8");
response.setHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(excelName, "UTF-8"));
out = new BufferedOutputStream(response.getOutputStream());
out.write(data);
out.flush();
out.close();
}
}
catch (IOException e) {
e.printStackTrace();
if (out != null)
try {
out.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
finally
{
if (out != null)
try {
out.close();
} catch (IOException e) {
}
}
}
public static void createTitle(HSSFRow row, List<String> pros){
HSSFCell cell = null;
for (int i = 0; i < pros.size(); i++) {
cell = row.createCell(i);
cell.setCellType(1);
cell.setCellValue((String)pros.get(i));
}
}
}