日期:2014-05-18  浏览次数:20787 次

excel中文转换问题
写了,以下代码,不知道为什么,写进excel中的中文就变成了乱码

      public   static   void   main(String[]   args)   {
                HSSFWorkbook   workbook   =   new   HSSFWorkbook();
                HSSFSheet   worksheet   =   workbook.createSheet( "work1 ");
                HSSFRow   row   =   worksheet.createRow(0);
                HSSFCell   cell   =   row.createCell((short)   0);
                cell.setCellStyle(workbook.createCellStyle());
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue( "你好 ");
                row   =   worksheet.createRow(1);
                cell   =   row.createCell((short)   0);
                cell.setCellValue( "热烈欢迎 ");
                try   {
                        FileOutputStream   fos   =   new   FileOutputStream( "c:/t.xls ");
                        workbook.write(fos);
                        fos.flush();
                        fos.close();
                        System.out.println( "----------------生成完毕 ");
                }   catch   (Exception   ex)   {
                        ex.printStackTrace();
                }

        }

------解决方案--------------------
poi有地方好像要设字符编码,设上了就OK了
------解决方案--------------------
HSSFCell 可以设置字符集
------解决方案--------------------
cell.setEncoding(cell.ENCODING_UTF_16);
------解决方案--------------------
设置好字体和编码就行了

HSSFFont font = wb.createFont();
font.setFontName( "SimSun "); //宋体
HSSFCellStyle style = wb.createCellStyle();
style.setFont(font);
HSSFCell cell = queryHeaderRow.createCell((short)0);
cell.setCellStyle(style);
cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
------解决方案--------------------
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell = null;

String[] title = { "部门 ", "工号 ", "姓名 ", "类型 ", "初始值 ", "已使用 ", "当前值 "};
for (int i = 0; i < title.length; i++) {
cell = row.createCell((short) i);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(cell.ENCODING_UTF_16);
cell.setCellValue(title[i]);