使用java apache POI读取和写入excel表

时间:2016-12-04 00:50:19

标签: java apache-poi

我正在使用java apache POI阅读excel表。读完之后我想写在同一行上,ode正在读完但不写我想在每次迭代的每一行创建第6列....这里是代码

 /**
 * 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();
    }

}

1 个答案:

答案 0 :(得分:0)

您只是在阅读文件而不是写文件。你必须在最后添加这样的东西:

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