java poi导出excel问题
XSSFWorkbook workbook = new XSSFWorkbook(); //创建一个excel
XSSFCell cell = null; //Excel的列
XSSFRow row = null; //Excel的行
XSSFSheet sheet = workbook.createSheet("sheet1"); //创建一个sheet
row = sheet.createRow(5);
Font font = workbook.createFont();
//表头部分
for(int k = 0;k < headlist.length;k++){
cell = row.createCell(k);//创建第0行第k列
cell.setCellValue(headlist[k]);//设置第0行第k列的值
int wid=4000;
if((k>4&&k<9)||k==15||k==21){
wid=6000;
}
if(k==7){
wid=10000;
}
sheet.setColumnWidth(k,wid);//设置列的宽度
font.setFontName("Arial Unicode MS");
font.setFontHeightInPoints((short)11);
font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
font.setColor(IndexedColors.BLACK.getIndex());
CellStyle style2 = workbook.createCellStyle();
style2.setFont(font);//设置字体风格
style2.setFillPattern(CellStyle.SOLID_FOREGROUND);
style2.setFillForegroundColor((short)IndexedColors.CORAL.getIndex());
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setBorderBottom((short)1);
style2.setBorderLeft((short)1);
style2.setBorderRight((short)1);
style2.setBorderTop((short)1);
cell.setCellStyle(style2);
}
for(int i = 0 ;i < dataList.size() ;i++){
String[] rowlist = dataList.get(i);//获取student对象
row = sheet.createRow((short) (i + 6));//创建第i+1行
row.setHeight((short)400);//设置行高
int k=0;
for (int j=0;j<rowlist.length;j++){
cell = row.createCell(k);
String cellstr=rowlist[j];
//Double cellDouble=0.00;
// if(j==0||j==3||j==9||j==13||j==14||j==18||j==23||j==24||j==26||j==28||j==30||j==32||j==34){
// cellDouble=Double.valueOf(cellstr);
// cell.setCellValue(cellDouble);
// }else{
// cell.setCellValue(cellstr);
//}
cell.setCellValue(cellstr.equals(" ")?"":cellstr);
font.setFontName("Arial Unicode MS");
font.setColor(IndexedColors.BLACK.getIndex()); // 设置单元格字体的颜色.
font.setFontHeightInPoints((short)10); //设置单元字体高度
font.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
CellStyle style = workbook.createCellStyle();
style.setFont(font);//设置字体风格
style.setBorderBottom((short)1);
style.setBorderLeft((short)1);
style.setBorderRight((short)1);
style.setBorderTop((short)1);
cell.setCellStyle(style);
k++;
}
}
HttpServletResponse response = null;//创建一个HttpServletResponse对象
OutputStream out = null;//创建一个输出流对象
try {
response = ServletActionContext.getResponse();//初始化HttpServletResponse对象
out = response.getOutputStream();//
String headerStr ="Store Master";
headerStr =new String(headerStr.getBytes("gb2312"), "ISO8859-1");//headerString为中文时转码
response.setHeader("Content-disposition","attachment; filename="+ headerStr+".xlsx");//filename是下载的xls的名,建议最好用英文
response.setContentType("application/msexcel;charset=UTF-8");//设置类型
response.setHeader("Pragma","No-cache");//设置头
response.setHeader("Cache-Control","no-cache");//设置头
response.setDateHeader("Expires", 0);//设置日期头
workbook.write(out);
out.flush();
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
}finally{
try{
if(out!=null){
out.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
代码如上,导出excel报错:有内容不可读取,求大神指导,谢谢