我有一个数组,我想从中检索值并使用 Jxl 将它们插入到 Excel 中,一个值插入一行,例如:
1| blah blah blah
2| blah blah blah
3| blah blah blah
我可以从数组中检索值,如下面的代码所示,但是将它们插入行对我来说很困难......我通过各种尝试管理的最好方法是插入正确数量的行,但所有相同的值(数组中存储的最后一个值)。
private static void InsertStrings() throws RowsExceededException, WriteException {
try {
System.out.println("Starting Write to Excel");
WritableWorkbook workbook = Workbook.createWorkbook(new File("C:\\Users\\Jason\\Documents\\Development\\Seaport Crawler\\CrawlerResults.xls"));
WritableSheet sheet = workbook.createSheet("Results", 0);
Label label = new Label(0, 0, "Notices to Mariners");
sheet.addCell(label);
for ( int indx = 0; indx < arr_Heading.size(); indx++)
{
/********HEADING and TITLE ARRAY*********/
String heading;
int rowValue = indx;
heading = arr_Heading.get(rowValue);
//Editing the Descriptions
if (heading.contains("-")) {
// Split it
String string = heading;
String[] parts = string.split("-",2);
String part1 = parts[0]; // Heading
String part2 = parts[1]; // Title
//String part1s = part1.replaceAll("\\s+","");//Replace Spaces with none
// String part2s = part2.replaceAll("\\s+","");
System.out.println("row Value........" + rowValue + " Heading... " + part1 + " Title... " + part2);
//Insert Strings to Excel Workbook
Label ntmHeading = new Label(2, indx, part1);
sheet.addCell(ntmHeading);
}else{
//something
}
// All sheets and cells added. Now write out the workbook
workbook.write();
workbook.close();
}
} catch (IOException e) {
System.out.println("WRITE TO EXCEL FAILED");
e.printStackTrace();
}
谢谢
最佳答案
在 for 循环之外编写以下语句:
workbook.write();
workbook.close();
对 try catch block /抛出进行一些调整,它可以工作。
关于java - 将数组插入 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23834353/