【问题标题】:Reading and writing excel sheet using java apache POI使用java apache POI读写excel表
【发布时间】:2017-04-18 15:55:09
【问题描述】:

我正在使用 java apache POI 阅读 excel 表。读完后我想写在同一行上,颂歌读得很好,但写得不好我想在每次迭代的每一行中创建第 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();
    }

}

【问题讨论】:

标签: java apache-poi


【解决方案1】:

您只是读取文件而不是写入文件。你必须把这样的东西放在最后:

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

【讨论】:

  • 请注意,您应该写入不同的文件,因为就地写入尚未(还)完全支持。
猜你喜欢
  • 2014-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多