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上,出现乱码。请大家帮帮忙。
// 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上,出现乱码。请大家帮帮忙。
解决方案 »
- excel的冷冻窗口
- 如何转换路径中的"\"?
- 哪里有错啊
- 如何实现类似于foxmail的提示栏?
- 各位大哥,救救小弟,给小弟我一点信心吧!我真的快要失去学习JAVA的信心了!
- ~~~~~~没想到每次到这时几乎都是散风,^_^ 继续散~~~~~
- EntityBean的select查询问题
- 如何在JDK下编JAVA和运行JAVA?
- 在线程a中使线程b结束,语句应怎样写?
- 请问(路人甲):我如何正确使用javax.sql,需要解开classes12.zip后把javax放入classpath中吗?
- 只有上下级关系,请教一个机构树的算法/
- 在java里面如何把2进制专成8进制,或者10进制,有具体的函数么
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;
}