乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > java 如何将字符串写入一个已有的<em>excel</em>表格中-poi excel 写入

java 如何将字符串写入一个已有的<em>excel</em>表格中-poi excel 写入

作者:乔山办公网日期:

返回目录:excel表格制作


poi的Sheet对象可以获取最后一行位置,如int lastRowNum = sheet.getLastRowNum(); 知道最后一行位置,就可以在最后一行后面开始加入新数据,如Row row = sheet.getRow(lastRowNum+1);

操作Excel,用POI这个类库比较多。 下面是示例代码7a64e4b893e5b19e364
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import java.io.*;

public class Sample3_1{
public static void main(String[] args){
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();

Row row1 = sheet.createRow(1);
Row row2 = sheet.createRow(2);

Cell cell1_0 = row1.createCell(0);
Cell cell1_1 = row1.createCell(1);
Cell cell1_2 = row1.createCell(2);

Cell cell2_0 = row2.createCell(0);
Cell cell2_1 = row2.createCell(1);
Cell cell2_2 = row2.createCell(2);

cell1_0.setCellValue(10);
cell1_1.setCellValue(-8.5);
cell1_2.setCellValue(3.14);

cell2_0.setCellValue("Hello");
cell2_1.setCellValue("表形式");
cell2_2.setCellValue("3.14");

FileOutputStream out = null;
try{
out = new FileOutputStream("sample3_1.xls");
wb.write(out);
}catch(IOException e){
System.out.println(e.toString());
}finally{
try {
out.close();
}catch(IOException e){
System.out.println(e.toString());
}
}
}
}

需要对Excel中的数据进行读取操作。



HSSFWorkbook wb=new HSSFWorkbook(); //创建一个Excel
HSSFSheet sheet=wb.createSheet("sheet1");//创建一个Sheet

HSSFRow row=sheet.createRow(0);//创建一个行

HSSFCell cell=row.createCell((short)0); //创建一个单元格
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("序号"); //设置值

cell=row.createCell((short)1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("姓");
这个是最简单的一个例子 ,下面的参考资料 你可以看看....
对于新使用者很不错, 介绍也很详细 ,希望能够帮到你

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网