用jxl做了个查询并将结果通过Excel导出的功能,但对输入输出流那一部分知识掌握不够,一直无法完成客户端查询导出并保存的功能。
package com.util;import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Vector;import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableFont;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;import com.xamedical.base.Prescription;
import com.xamedical.common.Format;
import com.xamedical.common.Globa;
import com.xamedical.common.UID;
import com.xamedical.system.District;
import com.xamedical.system.SysUserUnit;
import com.xamedical.system.SysPara;
import com.xamedical.system.Unit;public class Excel {
private Globa globa;
    private HttpServletRequest request;
    private HttpServletResponse response;
    private HttpSession session;
    private ServletContext application; public Excel(Globa globa) {
this.globa = globa;
        this.request = globa.request;
        this.response = globa.response;
        this.session = globa.session;
        this.application = globa.application;
}

public void output() {
String strFileId = UID.getID();
//File fExcel = new File(application.getRealPath("") + "\\excel\\" + strFileId + ".xls");
String filename = "方剂.xls"
try {
                        //输出

OutputStream os = response.getOutputStream();
response.setHeader( "Content-Disposition", "attachment;filename="  + new String(filename.getBytes(),"ISO8859-1"));
response.setContentType("application/msexcel");      
WritableWorkbook  wwb = Workbook.createWorkbook(os);
jxl.write.WritableSheet ws = wwb.createSheet("复合查询结果", 0);
WritableFont wfSongBlack = new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.NO_BOLD, 
false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
jxl.write.WritableCellFormat wcfSongBlack = new jxl.write.WritableCellFormat(wfSongBlack);//获取需要导出的方剂记录集合
Vector<Prescription> content = (Vector<Prescription>)session.getAttribute("content");
try {
Label l;
String composition;
String[] array;
for(int i=0;i<content.size();i++) {
l = new Label(0,i,content.get(i).getStrName(),wcfSongBlack);
ws.addCell(l);
//分割方剂组成字符串,得到中药的一个字符串数组。
composition = content.get(i).getStrComposition();
composition = composition.replaceAll(",|。", "");
array = composition.split("\\d+g|.钱|.两");
for(int j=0;j<array.length;j++) {
l = new Label(j+1,i,array[j],wcfSongBlack);
ws.addCell(l);
}
}

} catch(WriteException we) {
we.printStackTrace();
}

wwb.write();//写入excel对象
wwb.close();//关闭可写入的Excel对象
os.close();

}catch (IOException ioe) {
ioe.printStackTrace();
}

}
}
输出结果全是乱码(R_>乨lS_R_抪臑獋}v/冇?)Y[r)Y臇輂巶竒^g3*NB),而且没有按设置好的单元格输出。

解决方案 »

  1.   

    不知你是想如何使用?
    如果是web方式的话,就在服务器上生成Excel文件,页面上提供链接,点击下载就可以了
    这样,就不需要
                response.setHeader( "Content-Disposition", "attachment;filename="  + new String(filename.getBytes(),"ISO8859-1"));
                response.setContentType("application/msexcel");                 
      

  2.   

    页面上提供一个按钮,点击就弹出下载对话框,参考过一部分代码,本质就是先导出Excel在服务器上,然后然后用ServletOutputStream往客户端写。具体代码:File fExcel = new File(application.getRealPath("") + "\\query\\" + strFileId + ".xls");response.setHeader( "Content-Disposition", "attachment;filename="  + new String(fExcel.getName().getBytes("gbk"), "ISO8859-1" ));
        BufferedInputStream fileInputStream = new BufferedInputStream(new FileInputStream(fExcel));
        ServletOutputStream output = response.getOutputStream();
        byte[] bytBuffer = new byte[1024 * 8];
        int i;
        while ((i = fileInputStream.read(bytBuffer)) != -1) {
            output.write(bytBuffer, 0, i);
        }
        fileInputStream.close();
        output.flush();
        output.close();
    // 删除
    fExcel.delete();
    主要是下载下来全是乱码。主要想问的是如何处理乱码?
      

  3.   

    response.setHeader( "Content-Disposition", "attachment;filename=" + new String(filename.getBytes(),"ISO8859-1"));
      response.setContentType("application/msexcel");