本人在学习使用selenium和UiAutomator的时候,学习了一excel文档的操作,前两天写了一个读取excel的方案,今天看了一下写入excel的,暂时用的Map<Integer,List<String[]>>作为写入源。现在分享出来,希望能对你有所帮助。
//写入xlsx文档public static void writeXlsx(String filename, Map<Integer,List<String[]>> map) {String fileType = filename.substring(filename.lastIndexOf(".") + 1, filename.length());//提取文件名后缀try {if (!fileType.equals("xlsx")) {//判断文件名是否正确output("文件名错误!");}XSSFWorkbook wb = new XSSFWorkbook();//新建工作区for(int sheetnum=0;sheetnum<map.size();sheetnum++){//遍历表格XSSFSheet sheet = wb.createSheet("第"+(sheetnum+1)+"个表格");List<String[]> list = map.get(sheetnum+1);//取出需要写入的表格内容,这里需要+1才行for(int i=0;i<list.size();i++){//遍历行XSSFRow row = sheet.createRow(i);//新建行String[] str = list.get(i);//取出需要写入的行信息for(int j=0;j<str.length;j++){//遍历写入行单元格XSSFCell cell = row.createCell(j);//创建单元格cell.setCellValue(str[j]);//写入单元格数据}}}FileOutputStream outputStream = new FileOutputStream(filename);//新建输出流wb.write(outputStream);//写入文件数据outputStream.close();//关闭输出流} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}
这里依然借鉴了前人的思路,自己根据需求优化了一些代码,把方法重新写了一遍。
使用Excel主要为了生成测试报告用的,在实际始终当中,效果一般般,Excel的普通格式比较简单,比如设置颜色和文字格式等等,但是设计合并单元格和插入信息等格式就复杂多了,后期已经放弃。
点击阅读原文,有兴趣的童鞋可以加QQ群交流
原文作者:FunTester;如有侵权,请联系管理员删除。