java中是可以自动生成excel文件的,其实他的原理很简单,就是你首先要提供给他一个excel的模板,然后代码会读取到这个excel,然后自动为其添加内容,再将其输出重定向一下。就实现了自动生成excel文件的功能。
Java中提供了很多jar包,生成.xls类型的excel文件的jar包和生成.xlsx文件类型的jar包不同,注意区别。.
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 static void parsePM25(File file, File path) throws Exception,
IOException {
//SQL语句查询数据
String sql2 = "SELECT DATEGRP,dq.siteid,dq.PM10_AQI pm10_aqi, dq.PM25_AQI pm25_aqi,DQ.PM10_DAY pm10_day,DQ.PM25_DAY pm25_day "
+ "FROM DQ_GRP_API_MV dq where dq.siteid=999 and DATEGRP = trunc(sysdate-1)";
//JDBC链接数据库
Statement st2 = conn.createStatement();
ResultSet rs2 = st2.executeQuery(sql2);
//初始化方法
POIFSFileSystem ps = new POIFSFileSystem(new FileInputStream(file));
HSSFWorkbook hwb = new HSSFWorkbook(ps);
//找到一个sheet页
HSSFSheet sheet = hwb.getSheetAt(0);
//初始化要填的行和列,要填写的数据
HSSFRow row = null;
HSSFCell valueCell1 = null;
HSSFCell valueCell2 = null;
HSSFCell valueCell3 = null;
HSSFCell valueCell4 = null;
HSSFCell valueCell5 = null;
String PM10_DAY = null;
String PM10_AQI = null;
String PM25_DAY = null;
String PM25_AQI = null;
//获取时间格式
DateFormat sd = new SimpleDateFormat("yyyy年M月d日");
DateFormat sd2 = new SimpleDateFormat("yyyyMMdd");
Date curDate = new Date(System.currentTimeMillis() - 1000 * 60 * 60
* 24);// 获取当前时间
String str = sd.format(curDate);
String str2 = sd2.format(curDate);
System.out.println(str);
//填写数据
row = sheet.getRow(1); //获取第一行
HSSFCell cell = row.getCell((short) 5);//获取第5列
cell.setCellValue(str);//设置数据
int c = 4;
while (rs.next()) {
row = sheet.getRow(c); //获取第c行
c++;
//站点号
valueCell1 = row.getCell((short) 1);//获取第1列
valueCell1.setCellValue(rs.getString("sitename"));//设置数据
// PM10_DAY
if (rs.getString("PM10_DAY") == null) {
PM10_DAY = "---";
} else {
PM10_DAY = rs.getString("PM10_DAY");
}
valueCell1 = row.getCell((short) 2);
valueCell1.setCellValue(PM10_DAY);
<---------------------------关于具体的填写内容就不详写了--------------------------------------->
//关闭链接
JDBCUtils.release(conn, st2, rs2);
//调用数据流方法写入,返回执行信息
String info = writeExcel(file2, hwb, "bjmemc_stat_" + str2 + ".xls");
//打印下执行的信息
System.out.println(info);
}
<---------------------------附上输出流方法--------------------------------------->
public static String writeExcel(File path, HSSFWorkbook hssfworkbook,
String filename) {
try {
BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(
new FileOutputStream(new File(path, filename)));
if (hssfworkbook != null) {
hssfworkbook.write(bufferedoutputstream);
}
bufferedoutputstream.close();
} catch (Exception e) {
e.printStackTrace();
}
return filename.toString();
}
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//其实生成.xlsx的文件和生成.xls文件的方法类似,区别只在于使用的jar包不同,下面方法的注释就不详写了
public static void parseAirQualityExcel(File file, File path) throws SQLException,
FileNotFoundException, IOException, InvalidFormatException {
//SQL语句查询数据
String sql2 = "SELECT DATEGRP,dq.siteid,dq.PM10_AQI pm10_aqi, dq.PM25_AQI pm25_aqi,DQ.PM10_DAY pm10_day,DQ.PM25_DAY pm25_day "
+ "FROM DQ_GRP_API_MV dq where dq.siteid=999 and DATEGRP = trunc(sysdate-1)";
//JDBC链接数据库
Statement st2 = conn.createStatement();
ResultSet rs2 = st2.executeQuery(sql2);
//初始化方法
OPCPackage ps= OPCPackage.open(file);XSSFWorkbook hwb = new XSSFWorkbook(ps);
XSSFSheet sheet = hwb.getSheetAt(0);
XSSFRow row = null;
XSSFCell cell1 = null;
int so2 = 0;
int c = 1;
while (rs.next()) {
row = sheet.getRow(c);
c++;
// SO2
if ((Object)rs.getInt("pm25") == null) {
so2 = 0;
} else {
so2 = rs.getInt("pm25");
}
cell1 = row.getCell((short) 1);
cell1.setCellValue(so2);
}
JDBCUtils.release(conn, st, rs);
String info = writeExcel1(path, hwb, "专报用图-" + str2 + ".xlsx");
System.out.println(info);
}
<---------------------------数据流方法类型不同,这里也附上输出流方法。--------------------------------------->
public static String writeExcel1(File path, XSSFWorkbook hssfworkbook,
String filename) {
try {
BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(
new FileOutputStream(new File(path, filename)));
if (hssfworkbook != null) {
hssfworkbook.write(bufferedoutputstream);
}
bufferedoutputstream.close();
} catch (Exception e) {
e.printStackTrace();
}
return filename.toString();
}