java - 追加excel值

时间:2009-11-25 06:03:12

标签: 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();
            }
        }

2 个答案:

答案 0 :(得分:0)

我看到每次调用该函数时都会创建一个新文件。相反,您需要打开现有文件,获取需要修改的工作表,然后修改这些值。 Bellow是打开文件并获取第一张表的代码:

try {
        inputStream = new FileInputStream(InputFile);
    }catch (FileNotFoundException e) {
        System.out.println ("File not found in the specified path.");
        e.printStackTrace ();
    }

    try {
        fileSystem = new POIFSFileSystem (inputStream);
    } catch (IOException e1) {
        System.out.println("Cannot create POIFSFileSystem object using the file specified!");
        e1.printStackTrace();
    }       

    try {
        workBook = new HSSFWorkbook(fileSystem);
    } catch (IOException e) {
        System.out.println("Cannot create HHSFWorkbook from POIFSFileSystem object");
        e.printStackTrace();
    }

    HSSFSheet sheet = workBook.getSheetAt(0);

在此之后,您必须修改所需的单元格并保存文件。

答案 1 :(得分:0)

请参阅下文,了解上述要求的完整功能(如果有点笨拙)。请特别注意:(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中文件中的数据。