使用java将数据的Arraylist写入excel
我想使用java将包含字符串ArrayList的数据写入excel。 当我只做第一个数据时,只将其写入excel。剩余的数据则不是使用java将数据的Arraylist写入excel,java,Java,我想使用java将包含字符串ArrayList的数据写入excel。 当我只做第一个数据时,只将其写入excel。剩余的数据则不是 public class ExcelGenerator { public static void main(String args[]) throws FileNotFoundException, HPSFException, IOException, SQLException, NestableException { ArrayLi
public class ExcelGenerator {
public static void main(String args[]) throws FileNotFoundException, HPSFException, IOException, SQLException, NestableException
{
ArrayList<String> arrlist = new ArrayList<String>();
arrlist.add("14");
arrlist.add("7");
arrlist.add("39");
arrlist.add("40");
/* For Loop for iterating ArrayList */
System.out.println("For Loop");
for (int counter = 0; counter < arrlist.size(); counter++) {
System.out.println(arrlist.get(counter));
}
HSSFWorkbook workbook=new HSSFWorkbook();
File f=new File("D:/Test/test.xls");
GeericExcelGenerator gl=new GeericExcelGenerator("Test",workbook);
FileOutputStream outputStream=new FileOutputStream(f);
gl.generate(outputStream,arrlist);
}
public void generate(OutputStream outputStream,ArrayList arrlist) throws SQLException, IOException, NestableException
{
try
{
int currentRow = 0;
for (int counter = 0; counter < arrlist.size(); counter++) {
HSSFRow row = sheet.createRow(currentRow);
System.out.println("Description is"+arrlist.get(counter));
String c=(String) arrlist.get(counter);
int i=0;
HSSFCell cell = HSSFCellUtil.createCell(row, i, null);
cell.setCellValue(c);
i++;
workbook.write(outputStream);
currentRow++;
currentRow++;
}
}catch(IOException e)
{}
finally {
outputStream.close();
}
}
}
公共类生成器{
publicstaticvoidmain(字符串args[])抛出FileNotFoundException、HPSFException、IOException、SQLException、NestableException
{
ArrayList arrlist=新的ArrayList();
添加(“14”);
添加(“7”);
添加(“39”);
添加(“40”);
/*用于迭代ArrayList的For循环*/
System.out.println(“For循环”);
对于(int counter=0;counter
尝试下面的解决方案可能是由于您的初始化“i”变量
public void generate(OutputStream OutputStream,ArrayList arrlist)
抛出SQLException、IOException、NestableException{
试一试{
int currentRow=0;
int i=0;
对于(int counter=0;计数器
或者,您可以使用下面的示例检查您的实现
您可以在IDE中使用格式化程序吗?这将使您的代码更易于阅读。如果您的程序不工作,我不会忽略异常,而是打印它们,因为它们可能试图告诉您一些事情。
工作表
变量突然从何而来?我哪儿都没看到。这不能编译。
public void generate(OutputStream outputStream, ArrayList arrlist)
throws SQLException, IOException, NestableException {
try {
int currentRow = 0;
int i = 0;
for (int counter = 0; counter < arrlist.size(); counter++) {
HSSFRow row = sheet.createRow(currentRow);
System.out.println("Description is" + arrlist.get(counter));
String c = (String) arrlist.get(counter);
HSSFCell cell = HSSFCellUtil.createCell(row, i, null);
cell.setCellValue(c);
i++;
workbook.write(outputStream);
currentRow++;
currentRow++;
}
} catch (IOException e) {
} finally {
outputStream.close();
}
}