exportxls >的filename属性怎么可以改成中文

如题所述

第1个回答  2016-01-26
类文件,导入poi包。

package com.grs.exportexcel;

import java.io.*;
import java.util.List;
import org.apache.poi.hssf.usermodel.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public final class ExcelUtils
{

public ExcelUtils() {
}

/**
* 导出excel对外接口
*
* @param title 标题 如:同业对标分段统计报表
* @param tableData 数据表数据 如:new String[][]{{"表头1","表头2"},{"data1","data2"}}
* @param exportFileName 导出后的文件名 如:data.xls
* @param request
* @param response
*/
public static void exportExcelData(
String title,
String[][] tableData,
String exportFileName,
HttpServletRequest request,
HttpServletResponse response) {
response.reset();
String fileName = "attachment;filename=\"" + exportFileName + "\";";
response.setHeader("Content-disposition", fileName);
response.setContentType("application/vnd.ms-excel");
try {
exportExcel(title,tableData,response.getOutputStream(),createWorkbook());
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (Exception e) {
e.printStackTrace();
// log.error(e.getMessage(), e);
}
}

protected static void exportExcel(
String title,
String[][] tableData,
OutputStream output,
HSSFWorkbook workbook)
throws Exception {
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFCellStyle titleStyle = workbook.createCellStyle();
titleStyle.setFillBackgroundColor((short) 55);
HSSFFont hsFont = workbook.createFont();
hsFont.setBoldweight((short) 700);
String titles[] = tableData[0];
titleStyle.setFont(hsFont);
titleStyle.setAlignment((short) 2);
HSSFRow row = sheet.createRow(1);

row.setHeight((short) 300);
HSSFCell cell = createCell(row, (short) 0, title, titleStyle);

row = sheet.createRow(3);
row.setHeight((short) 250);
short i = 0;
for (int forI = 0; forI < titles.length; forI++) {
createCell(row, i, titles[forI], titleStyle);
i++;
}

int rowCount = 4;

for (int rowPos = 1; rowPos < tableData.length; rowPos++) {
row = sheet.createRow(rowCount++);
i = 0;
for (int colPos = 0; colPos < tableData[rowPos].length; colPos++) {
createCell(row, i, tableData[rowPos][colPos], null);
i++;
}
}
try {
workbook.write(output);
} catch (Exception e) {
//throw new Exception(e);
}
}

protected static HSSFCell createCell(
HSSFRow row,
short cellCount,
String value,
HSSFCellStyle titleStyle) {
HSSFCell cell = row.createCell(cellCount);
cell.setEncoding((short) 1);
if (titleStyle != null)
cell.setCellStyle(titleStyle);
cell.setCellValue(value);

return cell;
}

public static HSSFWorkbook createWorkbook() throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
return wb;
}

public static HSSFWorkbook createWorkbook(int sheetCount)
throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
for (int i = 0; i < sheetCount; i++) {
HSSFSheet si = wb.createSheet();
}

return wb;
}

public static HSSFWorkbook createWorkbook(int sheetCount, List sblxList)
throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
for (int i = 0; i < sheetCount; i++) {
HSSFSheet si = wb.createSheet((String) sblxList.get(i));
wb.setSheetName(i, (String) sblxList.get(i), (short) 1);
}

return wb;
}

}

页面代码!

<%
//导出在数据
String queryData[][] = new String[3][2]; //[行][列]
//列名
/***
***在此处获得请求在参数,根据参数或者调用方法得到结果
***将结果存入queryDate[][]中
***/

String exportFileName = "data.xls";//导出Excel在名称可以根据参数命名。
//导出EXCEL
ExcelUtils.exportExcelData("san", queryData, exportFileName,
request, response);
if (out != null && out.getBufferSize() != 0) {
out.clearBuffer();
out = pageContext.pushBody();
}
%>