ServletOutputStream outputstream = response.getOutputStream();
// BufferedOutputStream buffer = new BufferedOutputStream(outputstream,4096);
boolean created_flag = false;
HCenterRecord record = new HCenterRecord();
HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.createSheet();
book.setSheetName(0, sheetname, HSSFWorkbook.ENCODING_UTF_16);
HSSFRow row = sheet.createRow((short) 0);
HSSFCellStyle style = book.createCellStyle();
style.setFillBackgroundColor(HSSFCellStyle.SQUARES);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setFillBackgroundColor(HSSFColor.GREEN.index);
style.setFillForegroundColor(HSSFColor.RED.index);
//设置EXCEL表格标题
for (int i = 0; i < title.length; i++) {
HSSFCell cell = row.createCell((short) i);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
style = book.createCellStyle();
//根据取出来的值放入EXCEL中
for (int j = 1; j <= count; j++) {
// if( j%35000==0 && j!=0 ){
// book.write(buffer);
// outputstream.flush();
// }
String[] s_dr_object = (String[]) vector.get(j - 1);
HSSFRow rowi = sheet.createRow((short) j);
for (int k = 0; k < title.length; k++) {
HSSFCell cell = rowi.createCell((short) k);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(s_dr_object[k]);
}
}
book.write(outputstream);
outputstream.flush();
buffer.flush();
buffer.close();
outputstream.close();
//return created_flag;
}
本人代码如上,可是当我数据量很大的时候,就出现异常益出,后来我用 // BufferedOutputStream buffer = new BufferedOutputStream(outputstream,4096); 但这样就不在显示下载页面,直接在IE上,出现乱码。请大家帮帮忙。

解决方案 »

  1.   

    下面是写入一行数据到Excel的程序,实践证明可以稳定使用
    public class ExportOperator {
      private   int  curRow = 0;  //当前行号,空行计算在内
      private HSSFRow poiRow = null;
      private   HSSFSheet poiSheet = null;
      private   HSSFWorkbook  poiWorkbook = null;
      // 用于储存用户设置的 EXCEL 文件一行的各字段内容
      private   String []  excelFields = null;....
    public static void main(String[] args) {
    File file = new File(fileName);
    if (file.exists()){
    file.delete();
    }
    try {
    file.createNewFile();
    outStream = new FileOutputStream(file);
               curRow = 0;
        poiWorkbook = new HSSFWorkbook();
                poiSheet = poiWorkbook.createSheet();
                poiWorkbook.setSheetName(0, "1");
    } catch (IOException e){
    }catch (FileNotFoundException e) {
    } catch (Exception e) {}    
    //初始化
    excelFields = new String [10];//比如有10列
    for (int i = 0; i < 10; i ++){
    excelFields[i] = new String("");
    }
    //赋值
    for (int j = 0; j < exportFieldList.size(); j++) {//要写到Excel中值存在exportFieldList中
       excelFields[i]  = (String) exportFieldList.get(j);
    } poiRow = poiSheet.createRow(curRow ++);
    for (short i = 0; i < excelFields.length; i ++){
        appendCell(excelFields [i], i);
        HSSFCell cell = null;
         cell = poiRow.createCell(i);
    cell.setEncoding(HSSFCell.ENCODING_UTF_16);//UTF8写入
    //设置字体
    HSSFFont font;
    font = poiWorkbook.createFont();
    font.setColor(HSSFFont.COLOR_NORMAL);
    font.setFontName("MS Pゴシック");
    HSSFCellStyle style = poiWorkbook.createCellStyle();
    style.setFont(font);
    cell.setCellStyle(style);
    //写入Excel
    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
    cell.setCellValue(excelFields [i]);
    }    
    poiWorkbook.write(outStream);
    poiWorkbook = null;
    }