11package edu .ucsd .library .xdre .utils ;
22
33import java .io .BufferedReader ;
4+ import java .io .ByteArrayInputStream ;
45import java .io .FileInputStream ;
56import java .io .FileOutputStream ;
67import java .io .IOException ;
78import java .io .InputStream ;
9+ import java .io .InputStreamReader ;
810import java .io .OutputStream ;
911import java .io .StringReader ;
1012import java .text .SimpleDateFormat ;
@@ -49,18 +51,16 @@ public HSSFWorkbook toExcel() throws IOException{
4951 workbook = new HSSFWorkbook ();
5052 sheet = workbook .createSheet ();
5153 }
52- StringReader reader = null ;
53- BufferedReader bf = null ;
54- try {
55- reader = new StringReader (csv );
56- bf = new BufferedReader (reader );
54+
55+ try (InputStream in = new ByteArrayInputStream (csv .getBytes ());
56+ InputStreamReader reader = new InputStreamReader (in , "UTF-8" );
57+ BufferedReader bf = new BufferedReader (reader );) {
5758 String line = null ;
5859 String [] tokens = null ;
5960 String delimiter = "\t " ;
6061 HSSFRow row = null ;
61- int rowCount = 0 ;//sheet.getLastRowNum() + 1;
62- //if(sheet.getRow(rowCount) != null)
63- // rowCount += 1;
62+ int rowCount = 0 ;
63+
6464 while ((line =bf .readLine ()) != null ){
6565 if (line .length () > 0 ){
6666 tokens = line .split (delimiter );
@@ -71,11 +71,6 @@ public HSSFWorkbook toExcel() throws IOException{
7171 rowCount ++;
7272 }
7373 }
74- }finally {
75- if (bf != null )
76- try {bf .close ();}catch (Exception e ){}
77- if (reader != null )
78- try {reader .close ();}catch (Exception e ){}
7974 }
8075 return workbook ;
8176 }
@@ -88,8 +83,7 @@ private void createRow(HSSFRow row, String[] tokens, HSSFWorkbook workbook){
8883 cell = row .getCell (i );
8984 if (cell == null )
9085 cell = row .createCell (i );
91- //if( i>0 )
92- // cell.getCellStyle().setAlignment(HSSFCellStyle.ALIGN_CENTER);
86+
9387 if (tokens [i ] == null )
9488 tokens [i ] = "" ;
9589
@@ -102,7 +96,7 @@ private void createRow(HSSFRow row, String[] tokens, HSSFWorkbook workbook){
10296 link .setAddress (url );
10397 cell .setHyperlink (link );
10498 }
105- HSSFRichTextString text = new HSSFRichTextString (tokens [i ]);
99+ HSSFRichTextString text = workbook . getCreationHelper (). createRichTextString (tokens [i ]);
106100 cell .setCellValue (text );
107101 }
108102 }
0 commit comments