I am new bee trying to play with poi to create excel sheet.
I am trying this code
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Workbook;
public class Test {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
String tittle[]={"a1","a2","a3"};
List<String[]> data= new ArrayList<String[]>();
data.add(new String[]{"value11","value12","value13"});
data.add(new String[]{"value21","value22","value23"});
byte[] excel=getExcelBytes(tittle, data, "test");
FileOutputStream fileOutputStream= new
FileOutputStream("e:\\temp\\test.xls");
fileOutputStream.write(excel);
}
public static byte[] getExcelBytes(String title[], List datalist,
String
sheetName){
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(sheetName);
HSSFRow[] row = new HSSFRow[datalist.size() + 1];
HSSFCell[] valueCell = new HSSFCell[title.length];
row[0] = sheet.createRow(0);
HSSFCell[] headerCell = new HSSFCell[title.length];
for (int i = 0; i < title.length; i++) {
headerCell[i] = row[0].createCell(i);
headerCell[i].setCellValue(title[i]);
}
Iterator iterator = datalist.iterator();
int n_row = 1;
while (iterator.hasNext()) {
String data[] = (String[]) iterator.next();
row[n_row] = sheet.createRow(n_row);
for (int n_cel = 0; n_cel < data.length; n_cel++) {
valueCell[n_cel] = row[n_row].createCell(n_cel);
valueCell[n_cel].setCellValue(data[n_cel]);
}
n_row++;
}
//sheet.setGridsPrinted(false);
return wb.getBytes();
}
}
when I open the generated excel it complains
Microsoft Office Excel has encountered a problem and needs to close. We are
sorry for the inconvenience.
about unexpected error and it restarts it does it for 2 times and repairs
excel by itself and opens it , please tell me what am I doing wrong ?
--
View this message in context:
http://apache-poi.1045710.n5.nabble.com/new-bee-error-when-opening-excel-tp3349074p3349074.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]