为什么我用POI导出的Excel文件中,中文全部都是乱码
请教高手解决问题

解决方案 »

  1.   

    我没用过poi ,但是我会jxl,给你个例子package test;import java.io.File;
    import java.io.IOException;
    import java.util.Locale;import jxl.CellView;
    import jxl.Workbook;
    import jxl.WorkbookSettings;
    import jxl.format.UnderlineStyle;
    import jxl.write.Formula;
    import jxl.write.Label;
    import jxl.write.Number;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    import jxl.write.biff.RowsExceededException;public class WriteExcel {
    private WritableCellFormat timesBoldUnderline;  
        private WritableCellFormat times;  
        private String inputFile;         public void setOutputFile(String inputFile) { 
        this.inputFile = inputFile;  
        }      public void write() throws IOException, WriteException {  
            File file = new File(inputFile);  
            WorkbookSettings wbSettings = new WorkbookSettings();  
            wbSettings.setLocale(new Locale("en", "EN"));  
            WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);  
            workbook.createSheet("Report", 0);  
            WritableSheet excelSheet = workbook.getSheet(0); 
            createLabel(excelSheet);  
            createContent(excelSheet);  
            workbook.write();  
            workbook.close();  
        }  
        private void createLabel(WritableSheet sheet)  throws WriteException { 
            // Lets create a times font  
            WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);  
            // Define the cell format  
            times = new WritableCellFormat(times10pt);  
            // Lets automatically wrap the cells  
            times.setWrap(true);  
            // Create create a bold font with unterlines  
            WritableFont times10ptBoldUnderline = new WritableFont(  
                    WritableFont.TIMES, 10, WritableFont.BOLD, false,  
                    UnderlineStyle.SINGLE);  
            timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);  
            // Lets automatically wrap the cells  
            timesBoldUnderline.setWrap(true);  
            CellView cv = new CellView();  
            cv.setFormat(times);  
            cv.setFormat(timesBoldUnderline); 
           // cv.setAutosize(true);  
            // Write a few headers  
            addCaption(sheet, 0, 0, "Header 1");  
            addCaption(sheet, 1, 0, "This is another header"); 
        }  
        private void createContent(WritableSheet sheet) throws WriteException,  RowsExceededException {  
            // Write a few number  
            for(int i = 1; i < 10; i++) {  
                // First column  
                addNumber(sheet, 0, i, i + 10);  
                // Second column  
                addNumber(sheet, 1, i, i * i);  
            }  
            // Lets calculate the sum of it  
            StringBuffer buf = new StringBuffer(); 
            buf.append("SUM(A2:A10)");  
            Formula f = new Formula(0, 10, buf.toString());  
            sheet.addCell(f);  
            buf = new StringBuffer();  
            buf.append("SUM(B2:B10)");  
            f = new Formula(1, 10, buf.toString());  
            sheet.addCell(f);  
            // Now a bit of text  
            for(int i = 12; i < 20; i++) {  
                // First column  
                addLabel(sheet, 0, i, "Boring text "+ i);  
                // Second column  
                addLabel(sheet, 1, i, "Another text");  
            } 
        }  
        private void addCaption(WritableSheet sheet, int column, int row, String s)  throws RowsExceededException, WriteException {  
            Label label;  
            label = new Label(column, row, s, timesBoldUnderline);  
            sheet.addCell(label);  
        }  
        private void addNumber(WritableSheet sheet, int column, int row,  
                Integer integer) throws WriteException, RowsExceededException {  
            Number number;  
            number = new jxl.write.Number(column, row, integer, times);  
            sheet.addCell(number);  
        }         private void addLabel(WritableSheet sheet, int column, int row, String s)  
                throws WriteException, RowsExceededException { 
            Label label;  
            label = new Label(column, row, s, times);  
            sheet.addCell(label);  
        }         public static void main(String[] args) throws WriteException, IOException {  
            WriteExcel test = new WriteExcel();  
            test.setOutputFile("c:/temp/lars.xls");  
            test.write();  
            System.out.println("Please check the result file under c:/temp/lars.xls "); 
        }  }
      

  2.   


    package test;import java.io.File;
    import java.io.IOException;import jxl.Cell;
    import jxl.CellType;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;public class ReadExcel {
    private String inputFile;      public void setInputFile(String inputFile) { 
            this.inputFile = inputFile;  
        }    public void read() throws IOException  {
            File inputWorkbook = new File(inputFile);  
            Workbook w;  
            try{  
                w = Workbook.getWorkbook(inputWorkbook);  
                // Get the first sheet  
                Sheet sheet = w.getSheet(0);  
                // Loop over first 10 column and lines  
                for(int j = 0; j < sheet.getColumns(); j++) { 
                    for(int i = 0; i < sheet.getRows(); i++) {  
                        Cell cell = sheet.getCell(j, i);  
                        CellType type = cell.getType();  
                        if(cell.getType() == CellType.LABEL) {  
                            System.out.println("I got a label " 
                                    + cell.getContents());  
                        }  
                        if(cell.getType() == CellType.NUMBER) {  
                            System.out.println("I got a number " 
                                    + cell.getContents());                      }  
                    }  
                }  
            } catch(BiffException e) {  
                e.printStackTrace();  
            }      }      public static void main(String[] args) throws IOException {  
            ReadExcel test = new ReadExcel();  
            test.setInputFile("c:/temp/lars.xls");  
            test.read();  
        }  
    }
      

  3.   

    //导出数据模型
    public ActionForward outFile_Qt_Gjxgwsqzlzzxx(ActionMapping actionMapping,ActionForm actionForm, HttpServletRequest request,HttpServletResponse resp) throws FileNotFoundException, IOException{
    resp.setContentType("text/html;charset=GBK");
    //声明一个工作薄
    HSSFWorkbook wb = new HSSFWorkbook();
    //生成一个表格
    HSSFSheet st = wb.createSheet();
    wb.setSheetName(0,"专利明细表", HSSFWorkbook.ENCODING_UTF_16);
    //生成一个行
    HSSFRow row = st.createRow(0);
    //生成一个列
    HSSFCell cell = row.createCell((short) 0);
    cell.setEncoding(HSSFCell.ENCODING_UTF_16);

            //设置表头格式
            HSSFFont titleFont=wb.createFont();
    //        titleFont.setColor(HSSFColor.VIOLET.index);
            titleFont.setFontHeightInPoints((short) 20);
            titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            //设置表头样式
            HSSFCellStyle titleStyle=wb.createCellStyle();
            titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
            titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
            titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            //把格式应用到样式
            titleStyle.setFont(titleFont);
            //表头合并单元格
    st.addMergedRegion(new Region(0,(short)0,0,(short)7));
    cell.setCellValue("专利明细表"); 
    //应用样式
    cell.setCellStyle(titleStyle);
    st.addMergedRegion(new Region(1,(short)0,1,(short)7));
    st.createRow(1).createCell((short)0).setCellValue("单位:万元");
    st.createRow(2).createCell((short)1).setCellValue("序号");
    st.createRow(2).createCell((short)2).setCellValue("资助单位名称");
    st.createRow(2).createCell((short)3).setCellValue("PCT申请号");
    st.createRow(2).createCell((short)4).setCellValue("发明名称");
    st.createRow(2).createCell((short)5).setCellValue("资助金额(万元)");
    st.createRow(2).createCell((short)6).setCellValue("账号");
    st.createRow(2).createCell((short)7).setCellValue("开户行名称");
    cell.setCellStyle(titleStyle);
    for(int i = 2;i<=5;i++){
    st.setColumnWidth((short) i, (short)5000);
    }

    FileOutputStream writeFle = new FileOutputStream("C:\\excel.xls");
    wb.write(writeFle);
    writeFle.close();
    return actionMapping.findForward("listChunk_Qt_Gjxgwsqzlzzxx");
    }
      

  4.   

    response.setContentType("application/vnd.ms-excel;charset=gb2312");
    编码按照你项目的编码改一下,试试
      

  5.   

    试试utf-8,这个一般不会有乱码
      

  6.   

    fileName=new  String(fileName.getBytes("ISO_8859_1"),"UTF-8");
      

  7.   

    我用的是POI导出成Excel: String driverName = "com.mysql.jdbc.Driver";  // 驱动名称
    String userName = "root";  // 用户名
    String password = "318404";  // 密码
    String dbName = "test";  // 数据库名字
    String tableName = "userinfo";  // 表名
    String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+password;
    Class.forName(driverName).newInstance();
    Connection connection = DriverManager.getConnection(url);
    Statement statement = connection.createStatement();
    ResultSet rs = statement.executeQuery("SELECT userName,passWord,age,email,address FROM userinfo "); HSSFWorkbook workbook = new HSSFWorkbook();   //创建新的Excel工作薄
    HSSFSheet sheet = workbook.createSheet("userInfo");   //在Excel工作薄中建工作表,名为缺省
    HSSFRow row = sheet.createRow((short)0); //在索引0的位置建行(最顶端的行)
    // 
    HSSFCell cell = row.createCell((short)0); //在索引0的位置建单元格
    // cell.setEncoding(HSSFCell.ENCODING_UTF_16); //定义单元格为字符串类型
    cell.setCellValue("编号");  //在单元格输入一些内容

    cell = row.createCell((short)1); 
    cell.setCellValue("姓名");  //在单元格输入一些内容

    cell = row.createCell((short)2); 
    cell.setCellValue("密码");  

    cell = row.createCell((short)3); 
    cell.setCellValue("年龄");  

    cell = row.createCell((short)4); 
    cell.setCellValue("邮箱"); 

    cell = row.createCell((short)5); 
    cell.setCellValue("地址"); 

    int i = 1;
    while(rs.next()){
    row = sheet.createRow((short)i); //在索引1的位置创建行(最顶端的行)
    cell = row.createCell((short)0); //在索引0的位置创建单元格(左上端)
    cell.setCellValue(i); //在单元格输入一些内容

    cell = row.createCell((short)1);
    cell.setCellValue(rs.getString(1)); //在单元格输入一些内容

    cell = row.createCell((short)2); 
    cell.setCellValue(rs.getString(2));

    cell = row.createCell((short)3); 
    cell.setCellValue(rs.getString(3));

    cell = row.createCell((short)4); 
    cell.setCellValue(rs.getString(4));

    cell = row.createCell((short)5);  
    cell.setCellValue(rs.getString(5));

    i++; 
    }
    String filename = application.getRealPath("/") + "test.xls";  //filename是工作薄的存放位置,存放在当前应用程序的根目录下
    FileOutputStream fOut = new FileOutputStream(filename); //新建输出文件流
    workbook.write(fOut); //把相应的Excel工作薄存盘
    fOut.flush();
    fOut.close(); //操作结束,关闭文件
    out.println("excel文件已经生成,存放在  <font color=red>" + filename + "</font>");