java - 附加Excel值

标签 java

现在,每当我调用此函数时,Excel 文件 workbook.xls 的 B3 单元格中的值都会更新。我需要更改此函数,以便每当我调用此函数时,不应创建新的 Excel 文件,但单元格值应按第一次调用时 A1、第二次调用时 A2、第三次调用时 A3 的顺序附加,依此类推。你能帮我解决这个问题吗?

private static void readFromFile(String filename) {
        // TODO Auto-generated method stub
        BufferedReader bufferedReader = null;

        try {

            //Construct the BufferedReader object
            bufferedReader = new BufferedReader(new FileReader(filename));

            String line = null;

            while ((line = bufferedReader.readLine()) != null) {
                //Process the data, here we just print it out

                HSSFWorkbook wb = new HSSFWorkbook();
                HSSFSheet sheet = wb.createSheet("new sheet");
                HSSFRow row = sheet.createRow(2);
                int s_row=1;
                row.createCell(s_row).setCellValue(line);
                s_row++;
            //   row.createCell(1).setCellValue(new Date());
                FileOutputStream fileOut = new FileOutputStream("c:\\workbook.xls");
                wb.write(fileOut);
                fileOut.close();



               // System.out.println(line);
            }

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the BufferedReader
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

最佳答案

请参阅下文,了解您上述要求的功能齐全(如果有点笨拙)的实现。特别请注意:(1) while 循环创建并填充单元格; (2) HSSFWorkbook、Sheet、Row 和 s_row 的初始化发生在 while 循环之外; (3) 文件一次写入磁盘,最后一次; (4) 单元格以索引 0 开始,而不是索引 1; (5) 需要指定CreateRow(0)来获取电子表格中的第一行; (6) 我使用了 Sheet 和 Row 来代替...您的里程可能会有所不同! :)

快乐编码:

import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import java.io.*;

public class SpreadSheet {

    /**
     * Reads text from a file line by line
     */
    public void readFromFile(String filename) {

        BufferedReader bufferedReader = null;
        HSSFWorkbook wb;
        Sheet sheet;
        Row row;
        Cell aCell;
        int s_row;
        FileOutputStream fileOut = null;

        try {

            //Construct the BufferedReader object
            bufferedReader = new BufferedReader(new FileReader(filename));

            String line = null;

            s_row = 0;
            wb = new HSSFWorkbook();
            sheet = wb.createSheet("new sheet");
            row = sheet.createRow(0);
            fileOut = new FileOutputStream("workbook.xls");

            while ((line = bufferedReader.readLine()) != null) 
            {        
                    aCell = row.createCell(s_row++);
                    aCell.setCellValue(line);
            }

            // Do this once
            wb.write(fileOut);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the BufferedReader
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
                if (fileOut != null)
                    fileOut.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new SpreadSheet().readFromFile("testinput.txt");
    }
}

我的 testinput.txt 文件包含以下数据:

1
2
3
4
5
6
7

运行上述 Java 代码后,Microsoft Excel 工作表在单元格 A1 到 G1 中包含文件中的数据。

关于java - 附加Excel值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1794963/

相关文章:

java - 如何将程序的输出写入Excel表格

java - 关闭 BufferedImage 以便我可以删除它?

java - Camel-Spring 测试时间路线排除

Java Swing 应用程序 - 如果调整大小设置为 false,则按钮不会出现

java - 如何从 .jnlp 文件中获取 .jar 文件?

java - 扫描仪在返回之前需要两次输入

java - 为什么我们有 SortedMap/SortedSet 时还需要 TreeMap/TreeSet?

Java - 鼠标超出窗口

java - 当我使用对话框时,RecyclerView 和 ListView 中的所有值都在变化

java - 在 Android 中使用 Exception 使应用程序崩溃