poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Seshu Rao <Seshu_...@bio-rad.com>
Subject UTF8 issue with HSSF
Date Wed, 09 Mar 2005 21:45:43 GMT
Hi ,

    I am trying to read UTF-8 encoded data into a Java String , and trying 

to write it to Excel. However, the data in Excel after the conversion 
appears to be in Ascii if I use HSSF, but it works file if I directly 
write to a csv file and then import it manually. I appreciate your help in 

this regard. Here is my code below.

import com.biorad.webapp.bizlogic.registration.*;
import java.io.*;
import java.lang.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.model.*;
try{
   // the following line prints ISO8859_1, which is the default encoding 
on my machine
   print(new OutputStreamWriter(new 
ByteArrayOutputStream()).getEncoding());

   // this reads data from the database
   BRUserManager usrmgr = new BRUserManager();
   BRUser user = usrmgr.getUserByOID(24443);

    // if I use Buffered Writer with UTF8 encoding to write to a csv file, 

and then import to Excel , it shows japanese characters properly.
   //Writer out = new BufferedWriter(new OutputStreamWriter(new 
FileOutputStream("abc.txt"),"UTF8"));
   //out.write(user.getFirstName());
   //out.close();


   //write to xls using HSSFWorkbook
   HSSFWorkbook wb = new HSSFWorkbook();
   HSSFSheet sheet = wb.createSheet("new sheet");
   wb.setSheetName(0,"new sheet",HSSFWorkbook.ENCODING_UTF_16);
   HSSFSheet sheet = wb.getSheetAt(0);

   // Create a row and put some cells in it. Rows are 0 based.
   HSSFRow row = sheet.createRow((short) 1);

   // Create a new font and alter it.
   HSSFFont font = wb.createFont();
   font.setFontHeightInPoints((short)12);
   //font.setFontName("Arial Unicode MS");

   // Fonts are set into a style so create a new one to use.
   HSSFCellStyle style = wb.createCellStyle();
   style.setFont(font);
    // Create a cell and put a value in it.
   HSSFCell cell = row.createCell((short) 1);
   cell.setCellValue(user.getFirstName());
   cell.setCellStyle(style);
   cell.setEncoding((short)HSSFCell.ENCODING_UTF_16);
   HSSFCell cell = row.createCell((short) 2);
   cell.setCellValue(user.getLastName());
   cell.setCellStyle(style);
   cell.setEncoding((short) HSSFCell.ENCODING_UTF_16);
    // Write the output to a file
   FileOutputStream fileOut = new FileOutputStream("workbook.xls");
   wb.write(fileOut);
   fileOut.close();
}
catch(Exception e)
{
  e.printStackTrace();
}

Seshu
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message