利用excel模板,将数据填充到excel中

package com.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class WriteDataToExcel {

public static void main(String[] args) {

writeExcel("C:\\Users\\admin\\Desktop\\测试.xls");

}

public static void writeExcel(String finalXlsxPath) {
OutputStream out = null;
// 读取Excel文档
File finalXlsxFile = createNewFile(finalXlsxPath);//复制模板,
Workbook workBook = null;
try {
workBook = getWorkbok(finalXlsxFile);
} catch (IOException e1) {
e1.printStackTrace();
}

// sheet 对应一个工作页 插入数据开始 ------
Sheet sheet = workBook.getSheetAt(0);
Row row6 = sheet.getRow(5);// 获取到第6行
Cell cell5 = row6.getCell(4);// 6行 5列
cell5.setCellValue("6行5列的值");
Cell cell6 = row6.getCell(5);// 6行 6列
cell6.setCellValue("6行6列的值");
Cell cell7 = row6.getCell(6);// 6行 7列
cell7.setCellValue("6行7列的值");
Cell cell8 = row6.getCell(7);// 6行 8列
cell8.setCellValue("6行8列的值");
Cell cell9 = row6.getCell(8);// 6行 9列
cell9.setCellValue("6行9列的值");
Cell cell10 = row6.getCell(9);// 6行 10列
cell10.setCellValue("6行10列的值");
Row row7 = sheet.getRow(6);
Cell cell11 = row7.getCell(4);// 7行 5列
cell11.setCellValue("7行5列的值");
//插入数据结束

try {
out = new FileOutputStream(finalXlsxFile);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
workBook.write(out);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
if (out != null) {
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}

}

/**
* 判断excel格式版本
*
* @param file
* @return
* @throws IOException
*/
public static Workbook getWorkbok(File file) throws IOException {
Workbook wb = null;
FileInputStream in = new FileInputStream(file);
if (file.getName().endsWith(".xls")) { // Excel 2003
wb = new HSSFWorkbook(in);
} else if (file.getName().endsWith("xlsx")) { // Excel 2007/2010
wb = new XSSFWorkbook(in);
}
return wb;
}

private static File createNewFile(String path) {
// 读取模板,并赋值到新文件************************************************************
// 文件模板路径

File file = new File(path);
if (!file.exists()) {
System.out.println("原模板文件不存在");
}
// 保存文件的路径
String realPath = file.getParent();
// 新的文件名
String newFileName = "报表-" + System.currentTimeMillis()
+ ".xls";
// 判断路径是否存在
File dir = new File(realPath);
if (!dir.exists()) {
dir.mkdirs();
}
// 写入到新的excel
File newFile = new File(realPath, newFileName);
try {
newFile.createNewFile();
// 复制模板到新文件
FileUtils.copyFile(file, newFile);
} catch (Exception e) {
e.printStackTrace();
}
return newFile;
}
}

jar:

poi-3.6-20091214.jar
poi-ooxml-3.6-20091214.jar
commons-io-1.4.jar

猜你喜欢

转载自www.cnblogs.com/woshuaile/p/9856527.html
今日推荐