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]);
}
List list = Holiday.query(conn, this.orgcode, this.stuffID, this.stuffName, this.stuffKind, this.year);
for(int i=0;i <list.size();i++){
Map map = (Map)list.get(i);
row = sheet.createRow((short) i + 1);
cell = row.createCell((short) 0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(cell.ENCODING_UTF_16);
cell.setCellValue(map.get( "orgname ")==null? " ":map.get( "orgname ").toString());
cell = row.createCell((short) 1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(cell.ENCODING_UTF_16);
cell.setCellValue(map.get( "stuffid ")==null? " ":map.get( "stuffid ").toString());
cell = row.createCell((short) 2);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(cell.ENCODING_UTF_16);
cell.setCellValue(map.get( "name ").toString());
cell = row.createCell((short) 3);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(cell.ENCODING_UTF_16);
cell.setCellValue(map.get( "type ")==null? " ":map.get( "type ").toString());
cell = row.createCell((short) 4);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(cell.ENCODING_UTF_16);
cell.setCellValue(map.get( "initial_value ")==null? " ":map.get( "initial_value ").toString());
cell = row.createCell((short) 5);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(cell.ENCODING_UTF_16);
cell.setCellValue(map.get( "kc_value ")==null? " ":map.get( "kc_value ").toString());
cell = row.createCell((short) 6);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setEncoding(cell.ENCODING_UTF_16);
cell.setCellValue(map.get( "current_value ")==null? " ":map.get( "current_value ").toString());
}
this.getResponse().reset();
this.getResponse().setContentType( "text/plain;charset=UTF-8 "); // UTF-8
this.getResponse().setBufferSize(1024 * 1024 * 10);
this.getResponse().setHeader( "Content-Disposition ", "attachment;filename=holiday.xls ");
ServletOutputStream out = this.getResponse().getOutputStream();
workbook.write(out);
out.flush();
我的代码片段。