import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
response.addHeader("content-type", "application/shlnd.ms-excel;charset=utf-8");
response.addHeader("content-disposition", "attachment; filename="+Excels.toUtf8String("中文文件名称")+".xls"); //文件名
//文件名称若是需要显示成中文,则需要将其转换成UTF-8的方式
Workbook workBook = Excels.createWorkbook(Excels.Excel_Type.EXCEL_97_2003);
Sheet sheet = workBook.createSheet("sheet显示的名称");//sheet名
//设置标题
List<String> tableHeadList = new ArrayList<String>();
tableHeadList.add("单位名称");
for (int i = 0; i < 10; i++)
{
tableHeadList.add("名称"+i);
}
Excels.createHeader_(workBook, tableHeadList, sheet, "标题名称 ", tableHeadList.size() - 1);
//设置内容
List<String> contentList = new ArrayList<String>();
for (int i = 0; i <10; i++)
{
contentList.add("A"+i);
Excels.createExcel(workBook, contentList, sheet, i + 2);
}
workBook.write(response.getOutputStream());
response.getOutputStream().close();
---------------------------------------------------------------------------------------------------------------------------------
//excels.java 文件
public static String toUtf8String(String s){
StringBuffer sb = new StringBuffer();
for (int i=0;i<s.length();i++){
char c = s.charAt(i);
if (c >= 0 && c <= 255){sb.append(c);}
else{
byte[] b;
try { b = Character.toString(c).getBytes("utf-8");}
catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}
public static void createHeader_(Workbook workbook,List<String> tableHeadList,Sheet sheet,String titleContent, int headCount)
{
//标题样式
Font titlefont = workbook.createFont();
titlefont.setFontHeightInPoints((short)12);
titlefont.setBoldweight(Font.BOLDWEIGHT_BOLD);
CellStyle title_style = workbook.createCellStyle();
title_style.setFont(titlefont);
title_style.setAlignment(CellStyle.ALIGN_CENTER);
//列头样式
Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
CellStyle head_style = workbook.createCellStyle();
head_style.setFont(font);
head_style.setAlignment(CellStyle.ALIGN_CENTER);
CreationHelper createHelper = workbook.getCreationHelper();
sheet.setDefaultColumnWidth(15);
//创建标题行
Row titleRow = sheet.createRow(0);
Cell titlecell = titleRow.createCell(0);
CellRangeAddress cellRange = new CellRangeAddress(0,(short)0,0,(short)headCount);
sheet.addMergedRegion(cellRange);
titlecell.setCellValue(createHelper.createRichTextString(titleContent));
titlecell.setCellStyle(title_style);
//创建列头
Row header = sheet.createRow(1);
for(int i=0;i<tableHeadList.size();i++) {
Cell cell = header.createCell(i);
cell.setCellValue(createHelper.createRichTextString((String)tableHeadList.get(i)));
cell.setCellStyle(head_style);
}
}
public static void createExcel(Workbook workbook,List<String> tableHeadList,Sheet sheet, int startRow)
{
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
CreationHelper createHelper = workbook.getCreationHelper();
sheet.setDefaultColumnWidth(15);
Row head = sheet.createRow(startRow);
for(int i=0;i<tableHeadList.size();i++) {
Cell cell = head.createCell(i);
cell.setCellValue(createHelper.createRichTextString((String)tableHeadList.get(i)));
cell.setCellStyle(cellStyle);
}
}