簡體   English   中英

使用Java Apache POI讀寫Excel工作表

[英]Reading and writing excel sheet using java apache POI

我正在使用Java Apache POI閱讀Excel工作表。 讀完后我想寫在同一行上,The ode讀得很好但不寫完,我想在每次迭代的每一行中創建第六列。...這是代碼

 /**
 * Created by Muhammad Hussain on 26/10/2016.
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.Scanner;

/**
 * A dirty simple program that reads an Excel file.
 * @author www.codejava.net
 *
 */
public class ReadExcel {

    public static void main(String[] args) throws IOException {
        String excelFilePath = "C:\\Users\\Muhammad Hussain\\Desktop\\Data-Collection.xlsx";
        FileInputStream inputStream = new FileInputStream(new File(excelFilePath));

        Workbook workbook = new XSSFWorkbook(inputStream);
        Sheet firstSheet = workbook.getSheetAt(4);
        Scanner input =new Scanner(System.in);
        for (int rowIndex = 1; rowIndex <= 5; rowIndex++) {

            Row row = firstSheet.getRow(rowIndex);
            Cell cell = row.getCell(3);
            String review = cell.getStringCellValue();
            System.out.println(review);
            String label =input.next();
            row.createCell(6).setCellValue(label);

        }

        inputStream.close();
        inputStream.close();
    }

}

您僅在讀取文件而未在寫入文件。 最后,您必須輸入以下內容:

FileOutputStream out = new FileOutputStream(new File("..."));
workbook.write(out);
out.close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM