1.Java后台生成指定路径下创建指定名称的CSV文件
java
/**
* <生成csv文件>
* @param filePath 文件路径名称
* @param fileName 文件名称
* @param colNameList 标题数据信息
* @param dataList CSV的文件数据
* @return filePath+fileName
* @throws
*/
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());
}
//先写入UTF-8-BOM编码头内容(防止用Excel文件打开CSV文件出现标题乱码情况)
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("关闭文件流失败");
}
}
}
/**
*将数据按行写入数据
*@param dataList 每一行的数据集合
*@param csvWreite
*@throws 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
/**
* 导出excel文件
* @param filePath 文件路径
* @param fileName 文件名称
* @param colNameList 标题名称
* @param dataList 每一页sheet数据列表
* @return
*/
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(); // 创建Workbook
for (Map<String, Object> map : dataList) {
//sheet的名称
String sheetName = MapUtils.getString(map, "sheetName");
//当前sheet的数据集合
List<List<String>> tempDataList = (List<List<String>>)MapUtils.getObject(map, "dataList");
Sheet sheet = workbook.createSheet(sheetName); // 创建Sheet
// 创建表头
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(); // 关闭Workbook释放资源
} catch (IOException e) {
log.error(" 关闭Workbook失败:"+e,e.getMessage());
}
}
return excelFile;
}