[英]Is there a way to print out two different arrays in two columns in Excel?
當我嘗試在 excel 文件的同一“工作表”中打印第二個數組“Object columnB []”時,第一個數組消失了嗎? 有什么方法可以輕松解決這個問題嗎? 我需要兩者,因為第一個數組是“固定的”。 第二個數組要復制多次,因為有不同的參賽者
package sprint2;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
public class ExcelTest {
public static void main(String[] args) throws IOException {
// Create workbook in .xls format
Workbook workbook = new HSSFWorkbook();
// For .xslx workbooks use XSSFWoorkbok();
// Create Sheets
Sheet sh = workbook.createSheet("Decathlon");
Sheet sh2 = workbook.createSheet("Heptathlon");
// cell.setCellValue("some string");
// Values for Column A for Decathlon
Object columnA[][] = { { "Name" },
{ "Number" },
{ "" },
{ "Event 1" },
{ "Event 2" },
{ "Event 3" },
{ "Event 4" },
{ "Event 5" },
{ "DAY 1 TOTAL" },
{ "DAY 1 PLACE" }, };
// Create Cells and Column A for Decathlon
int rowCount = 0;
for (Object emp[] : columnA) {
Row row = sh.createRow(rowCount++);
int columnCount = 0;
for (Object value : emp) {
Cell cell = row.createCell(columnCount++);
if (value instanceof String)
cell.setCellValue((String) value);
if (value instanceof Integer)
cell.setCellValue((Integer) value);
if (value instanceof Boolean)
cell.setCellValue((Boolean) value);
}
}
//-----------------------------------------------------------------------------------------
// Values for Column B for Decathlon
Object columnB[][] = { { "Calvin hall" },
{ 100 },
{ "RESULT", "SCORE" },
{ "E1 Result", "E1 Score" },
{ "E2 Result", "E2 Score" },
{ "E3 Result", "E3 Score" },
{ "E4 Result", "E4 Score" },
{ "E5 Result", "E5 Score" },
{ "D1 TOTAL" },
{ "D1 PLACE" }, };
// Create Row and Column B for Decathlon
int rowCount1 = 0;
for (Object emp1[] : columnB) {
Row row1 = sh.createRow(rowCount1++);
int columnCount1 = 1;
for (Object value1 : emp1) {
Cell cell1 = row1.createCell(columnCount1++);
if (value1 instanceof String)
cell1.setCellValue((String) value1);
if (value1 instanceof Integer)
cell1.setCellValue((Integer) value1);
if (value1 instanceof Boolean)
cell1.setCellValue((Boolean) value1);
}
}
// Merge cells on row 1,2,9,10 Column B and C
sh2.addMergedRegion(new CellRangeAddress(0, 0, 1, 2));
sh2.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
sh2.addMergedRegion(new CellRangeAddress(8, 8, 1, 2));
sh2.addMergedRegion(new CellRangeAddress(9, 9, 1, 2));
// Autosize Decathlon-columns
for (int i = 0; i < 16; i++) {
sh.autoSizeColumn(i);
}
try {
// Write the output to file
FileOutputStream output = new FileOutputStream("C:\\Users\\snick\\Desktop\\Exceltest.xls");
workbook.write(output);
output.close();
workbook.close();
System.out.println("Excel-file is Completed");
} catch (Exception e) {
e.printStackTrace();
}
}
}
迭代 object 時不要重新創建同一行 B. 替換
Row row1 = sh.createRow(rowCount1++);
和
Row row1 = sh.getRow(rowCount1++);
但是,您必須確保該行確實存在,即 columnA 數組的行數至少與 columnB 數組的行數相同。 否則,當 rowCount1 大於 columnA.length 時,您需要使用 createRow。 您可以先嘗試getRow
,如果它返回 null - 使用createRow
添加新的。
第二個循環中的createCell
調用也存在類似問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.