admin 管理员组

文章数量: 887016

测试所需导入Excel表的批量生成

        做测试的经常会遇到的就是做参数化,那么需要进行导入Excel表的测试,也是需要成千上万的不重复Excel表,手动去创建表格并填写内容不现实。今天分享一个一直在用的,生成Excel方法。

        我们项目中所需Excel表的要求,就是一个人代表一个Excel,一个人的名下有一千条产品,每个产品使用不同的标号来表示。

       首先自行下载POI的jar包,先来看main方法,主要的生成逻辑就有了,我们是控制Excel名称限制在202100000-202199999,再使用for循环限定生成多少个Excel文件。 

 接下来看一下我们的模板文件,需要我们从第五行开始录入信息,第一列的序号是从1-1000,我使用同一个人,不同的标识号。

 接下来运行代码看结果:

         能够正确生成,得到想要的批量的Excel表格,附上代码,根据不同的需求,改一下生成逻辑就可以。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;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;public class ExcelUtils {private File createExcelFile(String path, String fileName) throws Exception {InputStream in = null;OutputStream out = null;File excelFile = createNewFile(path, fileName);// System.out.println(excelFile.getName());// 模版File templateFile = new File(path + "/template" + "/模板.xls");//读Excel的模板文件if (!templateFile.exists())throw new Exception("模版文件不存在");// System.out.println(templateFile.getName());try {in = new BufferedInputStream(new FileInputStream(templateFile), 1024);out = new BufferedOutputStream(new FileOutputStream(excelFile), 1024);byte[] buffer = new byte[1024];int len;while ((len = in.read(buffer)) != -1) {out.write(buffer, 0, len);out.flush();}} finally {if (in != null)in.close();if (out != null)out.close();}return excelFile;}/* 初始化excel文件 */private void initExcelFile(File excelFile, String prefix) throws Exception {InputStream is = null;OutputStream out = null;HSSFWorkbook workbook = null;HSSFSheet sheet = null;is = new FileInputStream(excelFile);workbook = new HSSFWorkbook(is);String suffix = "";// 获取第一个sheetsheet = workbook.getSheetAt(0);if (sheet != null) {// 写数据for (int i = 4; i < 1004; i++) {//第五行开始录入信息HSSFRow row = sheet.createRow(i);HSSFCell cell = row.createCell(0);int j = i - 4;if (j < 10) {suffix = "000" + j;} else if (j < 100) {suffix = "00" + j;} else if (j < 1000) {suffix = "0" + j;} else {suffix = j + "";}cell.setCellValue(i - 3);//第一列序号1-1000cell = row.createCell(1);cell.setCellValue("张三");//第二列被保险人姓名cell = row.createCell(2);cell.setCellValue("身份证");//第三列证件类型cell = row.createCell(3);cell.setCellValue("123456789123456789");//第四列身份证号//...cell = row.createCell(6);			//suffix是0000-9999cell.setCellValue(prefix + suffix);//第7列,生成不重复的标号如2021000000000-2021000009999}out = new FileOutputStream(excelFile);workbook.write(out);}out.flush();out.close();}private File createNewFile(String path, String fileName) throws Exception {File newFile = new File(path, fileName);if (!newFile.exists())newFile.createNewFile();return newFile;}public static void main(String[] args) throws Exception {String path = "d:/ExcelFiles";//文件路径String fileName = "";//表格名称.xlsString prefix = "";//表格名称String tmpStr = "";FileWriter fw = new FileWriter("D:/ExcelFiles/template/filename.txt", true);//Excel表格名写入位置,方便测试做参数化BufferedWriter bw = new BufferedWriter(fw);long t0 = System.currentTimeMillis();//开始时间for (int j = 0; j < 1000; j++) {if (j < 100000 && j >= 10000) {tmpStr = "" + j;} else if (j < 10000 && j >= 1000) {tmpStr = "0" + j;} else if (j < 1000 && j >= 100) {tmpStr = "00" + j;} else if (j < 100 && j >= 10) {tmpStr = "000" + j;} else if (j < 10 && j >= 0) {tmpStr = "0000" + j;}// prefix是Excel名称prefix = "2021" + tmpStr;fileName = prefix + ".xls";ExcelUtils eu = new ExcelUtils();bw.write(prefix + "\n");System.out.println("正在创建 " + fileName + "文件..");File f = eu.createExcelFile(path, fileName);eu.initExcelFile(f, prefix);}bw.close();fw.close();long t1 = System.currentTimeMillis();//结束时间System.out.println("耗时:" + (t1 - t0) / 1000 + "秒钟");//打印耗时情况}
}

 相互学习,共同进步!

本文标签: 测试所需导入Excel表的批量生成