package Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Test {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
String path="E:\\test.xlsx";
if(path.contains("xlsx"))
{
XlsxExport(path);
}
else {
XlsExport(path);
}
}
public static void XlsxExport(String path)
{
File fi=new File(path);
XSSFWorkbook wb;
try {
wb = new XSSFWorkbook(new FileInputStream(fi));
XSSFSheet sheet = wb.getSheetAt(0);
//在相应的单元格进行赋值
XSSFCell cell = sheet.createRow(1).createCell(0);
cell.setCellValue("测试");
//修改模板内容导出新模板
FileOutputStream out = new FileOutputStream(path);
wb.write(out);
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void XlsExport(String path)
{
File fi=new File(path);
POIFSFileSystem fs;
try {
fs = new POIFSFileSystem(new FileInputStream(fi));
//读取excel模板
HSSFWorkbook wb = new HSSFWorkbook(fs);
//读取了模板内所有sheet内容
HSSFSheet sheet = wb.getSheetAt(0);
//在相应的单元格进行赋值
HSSFCell cell = sheet.createRow(1).createCell(0);
cell.setCellValue("测试");
//修改模板内容导出新模板
FileOutputStream out = new FileOutputStream(path);
wb.write(out);
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Test {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
String path="E:\\test.xlsx";
if(path.contains("xlsx"))
{
XlsxExport(path);
}
else {
XlsExport(path);
}
}
public static void XlsxExport(String path)
{
File fi=new File(path);
XSSFWorkbook wb;
try {
wb = new XSSFWorkbook(new FileInputStream(fi));
XSSFSheet sheet = wb.getSheetAt(0);
//在相应的单元格进行赋值
XSSFCell cell = sheet.createRow(1).createCell(0);
cell.setCellValue("测试");
//修改模板内容导出新模板
FileOutputStream out = new FileOutputStream(path);
wb.write(out);
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void XlsExport(String path)
{
File fi=new File(path);
POIFSFileSystem fs;
try {
fs = new POIFSFileSystem(new FileInputStream(fi));
//读取excel模板
HSSFWorkbook wb = new HSSFWorkbook(fs);
//读取了模板内所有sheet内容
HSSFSheet sheet = wb.getSheetAt(0);
//在相应的单元格进行赋值
HSSFCell cell = sheet.createRow(1).createCell(0);
cell.setCellValue("测试");
//修改模板内容导出新模板
FileOutputStream out = new FileOutputStream(path);
wb.write(out);
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}