CsvToXlsxUtil.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package com.winhc.task.util;
  2. import com.alibaba.fastjson.JSON;
  3. import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
  4. import com.winhc.task.bean.Company;
  5. import com.winhc.task.bean.Student;
  6. import org.apache.poi.xssf.usermodel.XSSFRow;
  7. import org.apache.poi.xssf.usermodel.XSSFSheet;
  8. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  9. import org.jumpmind.symmetric.csv.CsvReader;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import java.io.*;
  13. import java.nio.charset.Charset;
  14. import java.nio.charset.StandardCharsets;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. /**
  18. * -----------------------maven 依赖----------------------------
  19. * CSV解析依赖包
  20. * <dependency>
  21. * <groupId>org.jumpmind.symmetric</groupId>
  22. * <artifactId>symmetric-csv</artifactId>
  23. * <version>3.5.19</version>
  24. * </dependency>
  25. * <p>
  26. * APACHE POI包
  27. * <dependency>
  28. * <groupId>org.apache.poi</groupId>
  29. * <artifactId>poi-ooxml</artifactId>
  30. * <version>3.17</version>
  31. * </dependency>
  32. * <p>
  33. * -----------------------------------------------------------
  34. *
  35. * @description
  36. */
  37. public class CsvToXlsxUtil {
  38. private static final Logger log = LoggerFactory.getLogger(CsvToXlsxUtil.class);
  39. //CSV常用分隔符,如需动态扩展设置成配置项
  40. private static final char[] DELIMITERS = {
  41. ',',
  42. ';',
  43. '\001',
  44. ' ',
  45. '\t',
  46. '|',
  47. '#',
  48. '&'
  49. };
  50. public static void main(String[] args) {
  51. String csvFile = "D:\\data\\山西省太原市小店区.csv";
  52. System.out.println(csvToXLSX(csvFile));
  53. }
  54. /**
  55. * 读取CSV文件并写入到XLSX文件中,默认编码
  56. *
  57. * @param csvFileAddress
  58. * @return
  59. */
  60. public static String csvToXLSX(String csvFileAddress) {
  61. return csvToXLSX(csvFileAddress, "GBK");
  62. }
  63. /**
  64. * 读取CSV文件并写入到XLSX文件中,指定CSV文件编码
  65. *
  66. * @param csvFileAddress
  67. * @param charset
  68. * @return
  69. */
  70. public static String csvToXLSX(String csvFileAddress, String charset) {
  71. String xlsxFileAddress = "";
  72. FileOutputStream fileOutputStream = null;
  73. try {
  74. //char delimiter = getDelimiter(csvFileAddress);
  75. //xlsxFileAddress = csvFileAddress.replace("csv","xlsx"); //xlsx file address
  76. xlsxFileAddress = csvFileAddress.split("\\.")[0] + ".xls";
  77. XSSFWorkbook workBook = new XSSFWorkbook();
  78. XSSFSheet sheet = workBook.createSheet(getSheetName(csvFileAddress));
  79. int RowNum = -1;
  80. CsvReader csvReader = new CsvReader(csvFileAddress, ',', Charset.forName(charset));
  81. while (csvReader.readRecord()) {
  82. RowNum++;
  83. System.out.println(RowNum);
  84. XSSFRow currentRow = sheet.createRow(RowNum);
  85. for (int i = 0; i < csvReader.getColumnCount(); i++) {
  86. currentRow.createCell(i).setCellValue(csvReader.get(i));
  87. }
  88. }
  89. fileOutputStream = new FileOutputStream(xlsxFileAddress);
  90. workBook.write(fileOutputStream);
  91. return getFileName(xlsxFileAddress);
  92. } catch (Exception e) {
  93. log.error("CsvToXlsxUtil exception :", e);
  94. } finally {
  95. try {
  96. fileOutputStream.close();
  97. } catch (IOException e) {
  98. log.error("CsvToXlsxUtil close FileOutputStream exception :", e);
  99. }
  100. }
  101. return getFileName(xlsxFileAddress);
  102. }
  103. public static void csvToXLSxPlus(String csvFilePath, String xlsxFilePath, String sheetName) {
  104. EasyExcelUtil easyExcelUtil = new EasyExcelUtil();
  105. //easyExcelUtil.init(xlsxFilePath, sheet, head);
  106. easyExcelUtil.init(xlsxFilePath, sheetName);
  107. try {
  108. int RowNum = -1;
  109. CsvReader csvReader = new CsvReader(csvFilePath, ',', StandardCharsets.UTF_8);
  110. csvReader.setSafetySwitch(false);
  111. csvReader.setUseTextQualifier(false);
  112. while (csvReader.readRecord()) {
  113. RowNum++;
  114. System.out.println(RowNum);
  115. List<List<String>> sumDataList = new ArrayList<>(1);
  116. List<String> dataList = new ArrayList<>(1);
  117. for (int i = 0; i < csvReader.getColumnCount(); i++) {
  118. // String col = csvReader.get(i);
  119. // if (StringUtils.isNotBlank(col) && col.length() > 100000L) {
  120. // //break;
  121. // dataList.add(csvReader.get(i).substring(0, 100000));
  122. // }
  123. dataList.add(csvReader.get(i));
  124. }
  125. sumDataList.add(dataList);
  126. easyExcelUtil.doExportExcel(sumDataList);
  127. }
  128. } catch (Exception e) {
  129. log.error("CsvToXlsxUtil exception :", e);
  130. } finally {
  131. try {
  132. easyExcelUtil.finish();
  133. } catch (Exception e) {
  134. log.error("CsvToXlsxUtil close FileOutputStream exception :", e);
  135. }
  136. }
  137. }
  138. public static void jsonToXLSxPlus(String csvFilePath, String xlsxFilePath, String sheetName, List<List<String>> heads) {
  139. EasyExcelUtil easyExcelUtil = new EasyExcelUtil();
  140. easyExcelUtil.init(xlsxFilePath, sheetName, heads);
  141. easyExcelUtil.init(xlsxFilePath, sheetName);
  142. int RowNum = -1;
  143. try {
  144. File file = new File(csvFilePath);
  145. InputStreamReader r = new InputStreamReader(new FileInputStream(file));
  146. BufferedReader reader = new BufferedReader(r);
  147. String content;
  148. do {
  149. RowNum++;
  150. System.out.println(RowNum);
  151. content = reader.readLine();
  152. if (StringUtils.isBlank(content)) {
  153. break;
  154. }
  155. List<Company> dataList = new ArrayList<>(1);
  156. Company company = JSON.parseObject(content, Company.class);
  157. dataList.add(company);
  158. easyExcelUtil.doExportExcel(dataList);
  159. } while (true);
  160. } catch (Exception e) {
  161. log.error("CsvToXlsxUtil exception :", e);
  162. } finally {
  163. try {
  164. easyExcelUtil.finish();
  165. } catch (Exception e) {
  166. log.error("CsvToXlsxUtil close FileOutputStream exception :", e);
  167. }
  168. }
  169. }
  170. /**
  171. * 设置excel文件的sheet名称
  172. * 获取CSV文件名作为Excel文件的sheet名称
  173. *
  174. * @param path
  175. * @return
  176. */
  177. private static String getSheetName(String path) {
  178. try {
  179. String[] file = getFileName(path).split("\\.");
  180. return file[0];
  181. } catch (Exception e) {
  182. log.error("CsvToXlsxUtil get sheet name exception : ", e);
  183. return "Sheet";
  184. }
  185. }
  186. /**
  187. * 根据资源路径切割获取文件名
  188. *
  189. * @param path
  190. * @return
  191. */
  192. private static String getFileName(String path) {
  193. String[] paths = path.contains("\\") ? path.split("\\\\") : path.split("/");
  194. return paths[paths.length - 1];
  195. }
  196. /**
  197. * 常用CSV分隔符数组遍历资源第一行,分隔的字段数多的为资源分隔符
  198. * 异常情况下默认用’,‘作为分隔符
  199. *
  200. * @param path 资源路径
  201. * @return
  202. */
  203. private static char getDelimiter(String path) {
  204. BufferedReader br = null;
  205. char delimiter = ',';
  206. try {
  207. br = new BufferedReader(new FileReader(path));
  208. String line = br.readLine();
  209. CsvReader csvReader;
  210. int columCount = 0;
  211. for (char delimiterTest : DELIMITERS) {
  212. csvReader = new CsvReader(getStringStream(line), delimiterTest, Charset.forName("UTF-8"));
  213. if (csvReader.readRecord()) {
  214. int newColumnCount = csvReader.getColumnCount();
  215. if (newColumnCount > columCount) {
  216. columCount = newColumnCount;
  217. delimiter = delimiterTest;
  218. }
  219. }
  220. }
  221. } catch (Exception e) {
  222. log.error("CsvToXlsxUtil get delimiter exception :", e);
  223. } finally {
  224. try {
  225. br.close();
  226. } catch (IOException e) {
  227. log.error("CsvToXlsxUtil get delimiter close BufferedReader exception :", e);
  228. }
  229. }
  230. return delimiter;
  231. }
  232. /**
  233. * 字符串转输入流
  234. * 把CSV文件第一行数据转成输入流
  235. *
  236. * @param sInputString
  237. * @return
  238. */
  239. private static InputStream getStringStream(String sInputString) {
  240. if (sInputString != null && !sInputString.equals("")) {
  241. try {
  242. ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());
  243. return tInputStringStream;
  244. } catch (Exception e) {
  245. log.error("CsvToXlsxUtil get StringStream exception :", e);
  246. }
  247. }
  248. return null;
  249. }
  250. }