编辑
2023-06-01
实用工具
00
请注意,本文编写于 672 天前,最后修改于 484 天前,其中某些信息可能已经过时。

目录

前提
引入依赖
业务
测试

前提

业务需求除excel文件下载还要需要实现dbf文件格式的下载,之前的excel是根据easyexcel无对象实现解析数据库视图,实现动态的读取视图字段和内容,现在这个dbf下载则就是在上次的基础上修改实现的,上次的案例在这边🏝️

引入依赖

xml
<!-- 读写dbf文件--> <dependency> <groupId>com.github.albfernandez</groupId> <artifactId>javadbf</artifactId> <version>1.11.2</version> </dependency>

业务

java
@Override public void downLoadExcel(HttpServletResponse response, ExcelRequest request) { ExportEntity entity=baseMapper.selectById(request.getExportId()); if (ObjectUtil.isNull(entity)){ throw new BusinessException(ResultCodeEnum.ERR_0x1000.getCode(),"没查询该excel"); } //json转成list集合 //列名的集合 List<List<String>> headList = JSON.parseObject(entity.getExcelHeadList(),List.class); //每行数据的集合 List<List<Object>> dataList =JSON.parseObject(entity.getExcelDataList(),List.class); if (request.getFileType()!=1){ try{ String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20"); response.setCharacterEncoding("utf-8"); if (request.getFileType()==2){ response.setContentType("text/csv"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".csv"); }else { response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); } EasyExcel.write(response.getOutputStream()).head(headList).autoCloseStream(Boolean.FALSE).sheet("sheet").doWrite(dataList); } catch (IOException e) { throw new BusinessException(ResultCodeEnum.ERR_0x1000.getCode(),e.getMessage()); } }else { try { response.setContentType("application/x-dbf"); String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".dbf"); //先需要设置一下表结构--列名 DBFField[] dbfFields =new DBFField[headList.size()]; for (int i=0; i<headList.size();i++) { dbfFields[i]=new DBFField(); dbfFields[i].setName(headList.get(i).get(0)); // dbf表 字符类型 的字段长度最大不能超过254 dbfFields[i].setType(DBFDataType.CHARACTER); dbfFields[i].setLength(headList.get(i).get(0).length()); } Charset defaultCharset = forName("GBK"); OutputStream fos=response.getOutputStream(); DBFWriter writer = new DBFWriter(fos,defaultCharset); writer.setFields(dbfFields); //然后一条一条的往里面插数据 for (int i = 0; i < dataList.size(); i++) { Object[] rowData = new Object[dataList.get(0).size()]; for (int j = 0; j < dataList.get(0).size(); j++) { rowData[j]=String.valueOf(dataList.get(i).get(j)).equals("null")?"":String.valueOf(dataList.get(i).get(j)); } writer.addRecord(rowData); } // 写入数据 writer.write(fos); fos.close(); } catch (IOException e) { e.printStackTrace(); } } }

dbf需要为每个列设置格式,但是由于我这是视图,而且列名高达两百多就不进行分列设置格式了,全部默认为字符串格式

测试

easyexcel可以直接改结尾格式,实现导出dbf但是那种的编码并不是dbf的,用dbf的框架读取并不能识别,所以导出成功后我们还需要进行测试

已下是测试方法,会在控制台输出dbf文件中的全部信息

java
@Slf4j public class dbf { public static void main(String[] args) { List<Map<String, String>> readDbf=new LinkedList<>(); try { readDbf=readDbf("D:\\dbf\\测试.dbf","GBK"); } catch (IOException e) { e.printStackTrace(); } System.out.println(Arrays.toString(readDbf.toArray())); } /** * 读dbf记录 * @param path * @return * @throws IOException */ public static List<Map<String, String>> readDbf(String path, String charsetName) throws IOException { List<Map<String, String>> rowList = new ArrayList<>(); DBFReader dbfReader = new DBFReader(new FileInputStream(path), Charset.forName(charsetName)); Object[] rowValues; while ((rowValues = dbfReader.nextRecord()) != null) { Map<String, String> rowMap = new HashMap<String, String>(); for (int i = 0; i < rowValues.length; i++) { rowMap.put(dbfReader.getField(i).getName(), String.valueOf(rowValues[i]).trim()); } rowList.add(rowMap); } dbfReader.close(); return rowList; }

本文作者:Weee

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!