package com.chenkang.mybatisplus;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.List;
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 ExcelUtils {
public static void copyExcel(String modelExcel, String newExcel, String newName) {
HSSFWorkbook wb = null;
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File file = new File(newExcel);
if (!file.exists()) {
file.mkdirs();
}
fis = new FileInputStream(modelExcel);
fos = new FileOutputStream(newExcel + "/" + newName);
wb = new HSSFWorkbook(fis);
wb.write(fos);
} catch (Exception e) {
System.err.println("复制excel表格异常1:" + e.getMessage());
} finally {
try {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
System.err.println("复制excel表格异常2:" + e.getMessage());
}
}
}
}
package com.chenkang.mybatisplus;
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;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class A {
public static void main(String[] args) {
try {
//模板文件的绝对路径
String modelExcel = "E:\aa.xls";
String fileName="aa.xls";
//复制模板
ExcelUtils.copyExcel(modelExcel, "E:\img\",fileName);
//新文件的路径
fileName="E:\img\"+fileName;
FileInputStream fs=new FileInputStream(fileName); //获取d://test.xls
POIFSFileSystem ps=new POIFSFileSystem(fs); //使用POI提供的方法得到excel的信息
HSSFWorkbook wb=new HSSFWorkbook(ps);
HSSFSheet sheet=wb.getSheetAt(0); //获取到工作表,因为一个excel可能有多个工作表
FileOutputStream out=new FileOutputStream(fileName); //向d://test.xls中写数据
HSSFRow row = sheet.createRow(23);
row.createCell(1).setCellValue("10:00");
row.createCell(2).setCellValue("10:00");
row.createCell(3).setCellValue("10:00");
row.createCell(4).setCellValue("10:00");
HSSFRow row2 = sheet.createRow(24);
row2.createCell(0).setCellValue("温度");
row2.createCell(1).setCellValue(new Double("25"));
row2.createCell(2).setCellValue(new Double("29"));
row2.createCell(3).setCellValue(new Double("35"));
row2.createCell(4).setCellValue(new Double("18"));
HSSFRow row3 = sheet.createRow(25);
row3.createCell(0).setCellValue("湿度");
row3.createCell(1).setCellValue("10");
row3.createCell(2).setCellValue("20");
row3.createCell(3).setCellValue("20");
row3.createCell(4).setCellValue("30");
out.flush();
wb.write(out);
out.close();
} catch (Exception e) {
}
}
}