package org.jeecg.modules.to.util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class TextUtil{
public static void main(String[] args) throws Exception{
inserSheetData();
}
private static void inserSheetData() throws Exception {
String filePath = "C:\\Users\\oshait24\\Desktop\\Tool基本信息表.xls";
FileInputStream fs=new FileInputStream(filePath);
POIFSFileSystem fileSystem = new POIFSFileSystem(fs);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileSystem);
HSSFSheet InsertSheet=hssfWorkbook.getSheetAt(0);
HSSFRow row=InsertSheet.getRow(0);
System.out.println("最后一行的行 "+InsertSheet.getLastRowNum());
FileOutputStream out=new FileOutputStream(filePath);
int lastRowNum = 6;
row=InsertSheet.getRow((short)(lastRowNum));
row.createCell(1).setCellValue("xiaoming");
row.createCell(2).setCellValue(24);
row.createCell(3).setCellValue("nan");
row.createCell(4).setCellValue("nanjing");
out.flush();
hssfWorkbook.write(out);
out.close();
System.out.println("物理列数="+row.getPhysicalNumberOfCells()+" 实际列数="+row.getLastCellNum());
}
}
定义设置向个单元格添加数据
package org.jeecg.modules.to.util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class TextUtil{
public static void main(String[] args) throws Exception{
inserSheetData("7,8","3,4","q,w");
}
private static void inserSheetData(String hang ,String lie,String shuju) throws Exception {
String filePath = "C:\\Users\\oshait24\\Desktop\\Tool基本信息表.xls";
FileInputStream fs=new FileInputStream(filePath);
POIFSFileSystem fileSystem = new POIFSFileSystem(fs);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileSystem);
HSSFSheet InsertSheet=hssfWorkbook.getSheetAt(0);
HSSFRow row=InsertSheet.getRow(7);
System.out.println("最后一行的行 "+InsertSheet.getLastRowNum());
FileOutputStream out=new FileOutputStream(filePath);
String[] hangs = hang.split(",");
String[] lies = lie.split(",");
String[] shujus = shuju.split(",");
for (int i = 0; i <hangs.length ; i++) {
for (int j = 0; j <lies.length ; j++) {
int a =Integer.parseInt(hangs[i]);
int b =Integer.parseInt(lies[j]);
String c =shujus[j];
row=InsertSheet.getRow((short)(a));
row.createCell(b).setCellValue("hahh");
System.out.println(a+b+c);
}
}
out.flush();
hssfWorkbook.write(out);
out.close();
System.out.println("物理列数="+row.getPhysicalNumberOfCells()+" 实际列数="+row.getLastCellNum());
}
}