java-附加excel值
现在,无论何时调用此函数,excel文件工作簿.xls的B3单元格中的值都会更新。我需要更改此函数,以便无论何时调用此函数,都不应创建新的excel文件,而是应按照第一次调用A1、第二次调用A2、第三次调用A3的顺序追加单元格值,依此类推。你能帮我做这个吗java-附加excel值,java,Java,现在,无论何时调用此函数,excel文件工作簿.xls的B3单元格中的值都会更新。我需要更改此函数,以便无论何时调用此函数,都不应创建新的excel文件,而是应按照第一次调用A1、第二次调用A2、第三次调用A3的顺序追加单元格值,依此类推。你能帮我做这个吗 private static void readFromFile(String filename) { // TODO Auto-generated method stub BufferedReader buf
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();
}
}
我看到,每次调用函数时,您都在创建一个新文件。相反,您需要打开现有文件,获取需要修改的图纸,然后修改值。 下面是打开文件并获取第一张图纸的代码:
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)而loop仅创建和填充单元格;(2) HSSF工作簿、工作表、行和s_行的初始化发生在while循环之外;(3) 文件在最后写入磁盘一次;(4) 单元格以索引0而不是索引1开始;(5) 您需要指定CreateRow(0)以获取电子表格中的第一行;(6)我使用的是床单和行……您的里程可能会有所不同!:) 快乐编码:
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");
}
}
My testinput.txt文件包含以下数据:
1
2
3
4
5
6
7
运行上述Java代码后,Microsoft Excel工作表将数据包含在单元格A1到G1中的文件中。BTW,对于那些在家玩的人,上述代码来自: