1.Java后台生成指定路径下创建指定名称的CSV文件
java">
public static File generateCsv(String filePath, String fileName,
List<String> colNameList, List<List<String>> dataList) throws IOException {
BufferedWriter csvWrite = null;
String fileRealPath = filePath +"/"+ fileName + ".csv";
try {
File csvFile = new File(fileRealPath);
if (!csvFile.exists()){
File parentFile = csvFile.getParentFile();
if (!parentFile.exists()){
if (parentFile.mkdirs()){
log.info("目录创建成功:"+parentFile.getAbsolutePath());
}else{
log.info("目录创建失败:"+parentFile.getAbsolutePath());
}
}
}
if (csvFile.createNewFile()){
log.info("文件创建成功:"+csvFile.getAbsolutePath());
}else{
log.info("文件创建失败:"+csvFile.getAbsolutePath());
}
byte[] utf8bom={(byte)0xef,(byte)0xbb,(byte)0xbf};
FileOutputStream fileOutputStream = new FileOutputStream(csvFile);
fileOutputStream.write(utf8bom);
csvWrite = new BufferedWriter(
new OutputStreamWriter(fileOutputStream, "UTF-8"), 1024);
write(colNameList, csvWrite);
for (List<String> dataPerRow : dataList) {
write(dataPerRow, csvWrite);
}
csvWrite.flush();
return csvFile;
}
catch (IOException e) {
log.error("csv文件生成失败,原因:", e);
throw new IOException("csv文件生成失败");
}
finally {
try {
if (null != csvWrite) {
csvWrite.close();
}
}
catch (IOException e) {
log.error("关闭文件流失败,原因:", e);
throw new IOException("关闭文件流失败");
}
}
}
private static void write(List<String> dataList, BufferedWriter csvWrite) throws IOException {
for (String data : dataList) {
StringBuffer buffer = new StringBuffer();
String rowStr = buffer.append("\"").append(data).append("\",").toString();
csvWrite.write(rowStr);
}
csvWrite.newLine();
}
2.Java后台生成指定路径下创建指定名称的xlsx文件
java">
public static File generateExcel(String filePath, String fileName,
List<String> colNameList, List<Map<String,Object>> dataList) throws IOException {
String fileRealPath = filePath +"/"+ fileName + ".xlsx";
File excelFile = new File(fileRealPath);
if (!excelFile.exists()){
File parentFile = excelFile.getParentFile();
if (!parentFile.exists()){
if (parentFile.mkdirs()){
log.info("目录创建成功:"+parentFile.getAbsolutePath());
}else{
log.info("目录创建失败:"+parentFile.getAbsolutePath());
}
}
}
if (excelFile.createNewFile()){
log.info("文件创建成功:"+excelFile.getAbsolutePath());
}else{
log.info("文件创建失败:"+excelFile.getAbsolutePath());
}
Workbook workbook = new XSSFWorkbook();
for (Map<String, Object> map : dataList) {
String sheetName = MapUtils.getString(map, "sheetName");
List<List<String>> tempDataList = (List<List<String>>)MapUtils.getObject(map, "dataList");
Sheet sheet = workbook.createSheet(sheetName);
Row headerRow = sheet.createRow(0);
for (int i = 0; i < colNameList.size(); i++) {
headerRow.createCell(i).setCellValue(colNameList.get(i));
}
if (tempDataList != null && tempDataList.size() > 0){
for (int i = 0; i < tempDataList.size(); i++) {
List<String> lineDataList = tempDataList.get(i);
Row row = sheet.createRow(i + 1);
for (int j = 0; j < lineDataList.size(); j++) {
row.createCell(j).setCellValue(lineDataList.get(j));
}
}
}
}
try (FileOutputStream fileOut = new FileOutputStream(excelFile)) {
workbook.write(fileOut);
} catch (IOException e) {
log.error("写入文件失败:"+e,e.getMessage());
} finally {
try {
workbook.close();
} catch (IOException e) {
log.error(" 关闭Workbook失败:"+e,e.getMessage());
}
}
return excelFile;
}