如何使用string列表编写Excel文件

我想写一个excel文件。 所有的写作细节都包含一个列表。 并将其设置为该方法的input参数。 请告诉我这样做的最好方法。

public static void writeExcelFile(List<String> combineLists){ XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Employee Data"); | | } 

input列表:

你的图像表示你有一个列表里面的string列表,因为你的input参数只有列表作为参数。 所以我会假设它是List<List<String>>

  FileOutputStream out = new FileOutputStream(new File("demo.xlsx")); XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Employee Data"); //I am creating and adding list just for illustration purpose only List<String> l1 = new ArrayList<String>(); l1.add(" l1 1"); l1.add("l1 2"); List<String> l2 = new ArrayList<String>(); l2.add(" l2 1"); l2.add("l2 2"); List<List<String>> list = new ArrayList<List<String>>(); list.add(l1); list.add(l2); Iterator <List<String>>i = list.iterator(); int rownum=0; int cellnum = 0; while (i.hasNext()) { List<String> templist = (List<String>) i.next(); Iterator<String> tempIterator= templist.iterator(); Row row = sheet.createRow(rownum++); cellnum = 0; while (tempIterator.hasNext()) { String temp = (String) tempIterator.next(); Cell cell = row.createCell(cellnum++); cell.setCellValue(temp); } } workbook.write(out); out.close(); workbook.close(); 

我希望这有帮助。