- 生成.txt文件
- 生成.csv文件
- 生成.xls文件
import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.List; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class CreateFileUtil { /** * 生成.TXT格式文件,行數幾乎無上限 */ public static boolean createTxtFile(List<Object[]> rows, String filePath, String fileName) { // 標記文件生成是否成功 boolean flag = true; try { // 含文件名的全路徑 String fullPath = filePath + File.separator + fileName + ".txt"; File file = new File(fullPath); if (file.exists()) { // 如果已存在,刪除舊文件 file.delete(); } file = new File(fullPath); file.createNewFile(); // 格式化浮點數據 NumberFormat formatter = NumberFormat.getNumberInstance(); formatter.setMaximumFractionDigits(10); // 設置最大小數位為10 // 格式化日期數據 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); // 遍歷輸出每行 PrintWriter pfp = new PrintWriter(file, "UTF-8"); //設置輸出文件的編碼為utf-8 for (Object[] rowData : rows) { StringBuffer thisLine = new StringBuffer(""); for (int i = 0; i < rowData.length; i++) { Object obj = rowData[i]; // 當前字段 // 格式化數據 String field = ""; if (null != obj) { if (obj.getClass() == String.class) { // 如果是字符串 field = (String) obj; } else if (obj.getClass() == Double.class || obj.getClass() == Float.class) { // 如果是浮點型 field = formatter.format(obj); // 格式化浮點數,使浮點數不以科學計數法輸出 } else if (obj.getClass() == Integer.class || obj.getClass() == Long.class || obj.getClass() == Short.class || obj.getClass() == Byte.class) { // 如果是整形 field += obj; } else if (obj.getClass() == Date.class) { // 如果是日期類型 field = sdf.format(obj); } } else { field = " "; // null時給一個空格占位 } // 拼接所有字段為一行數據,用tab鍵分隔 if (i < rowData.length - 1) { // 不是最后一個元素 thisLine.append(field).append("\t"); } else { // 是最后一個元素 thisLine.append(field); } } pfp.print(thisLine.toString() + "\n"); } pfp.close(); } catch (Exception e) { flag = false; e.printStackTrace(); } return flag; } /** * 生成.csv格式文件,行數幾乎無上限 */ public static boolean createCsvFile(List<Object[]> rows, String filePath, String fileName) { // 標記文件生成是否成功 boolean flag = true; // 文件輸出流 BufferedWriter fileOutputStream = null; try { // 含文件名的全路徑 String fullPath = filePath + File.separator + fileName + ".csv"; File file = new File(fullPath); if (!file.getParentFile().exists()) { // 如果父目錄不存在,創建父目錄 file.getParentFile().mkdirs(); } if (file.exists()) { // 如果已存在,刪除舊文件 file.delete(); } file = new File(fullPath); file.createNewFile(); // 格式化浮點數據 NumberFormat formatter = NumberFormat.getNumberInstance(); formatter.setMaximumFractionDigits(10); // 設置最大小數位為10 // 格式化日期數據 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); // 實例化文件輸出流 fileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "GB2312"), 1024); // 遍歷輸出每行 Iterator<Object[]> ite = rows.iterator(); while (ite.hasNext()) { Object[] rowData = (Object[]) ite.next(); for (int i = 0; i < rowData.length; i++) { Object obj = rowData[i]; // 當前字段 // 格式化數據 String field = ""; if (null != obj) { if (obj.getClass() == String.class) { // 如果是字符串 field = (String) obj; } else if (obj.getClass() == Double.class || obj.getClass() == Float.class) { // 如果是浮點型 field = formatter.format(obj); // 格式化浮點數,使浮點數不以科學計數法輸出 } else if (obj.getClass() == Integer.class || obj.getClass() == Long.class || obj.getClass() == Short.class || obj.getClass() == Byte.class) { // 如果是整形 field += obj; } else if (obj.getClass() == Date.class) { // 如果是日期類型 field = sdf.format(obj); } } else { field = " "; // null時給一個空格占位 } // 拼接所有字段為一行數據 if (i < rowData.length - 1) { // 不是最后一個元素 fileOutputStream.write("\"" + field + "\"" + ","); } else { // 是最后一個元素 fileOutputStream.write("\"" + field + "\""); } } // 創建一個新行 if (ite.hasNext()) { fileOutputStream.newLine(); } } fileOutputStream.flush(); } catch (Exception e) { flag = false; e.printStackTrace(); } finally { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return flag; } /** * 生成.xls格式文件,單頁上限: 03版是65536行 ,07版的是1048576行, 10版不知 */ public static boolean createXlsFile(List<Object[]> rows, String filePath, String fileName) { // 標記文件生成是否成功 boolean flag = true; try { // 創建一個webbook,對應一個Excel文件 XSSFWorkbook wb = new XSSFWorkbook(); // 在webbook中添加一個sheet,對應Excel文件中的sheet XSSFSheet sheet = wb.createSheet(fileName); // 遍歷輸出每行 for (int i = 0; i < rows.size(); i++) { Object[] rowData = rows.get(i); // 每一行的數據 XSSFRow row = sheet.createRow(i); for (int j = 0; j < rowData.length; j++) { XSSFCell cell = row.createCell(j); // 假設只有三種類型的數據 if (rowData[j].getClass() == String.class) { // String類型數值 cell.setCellValue((String) rowData[j]); } else if (rowData[j].getClass() == double.class) { // double類型數值 cell.setCellValue((Double) rowData[j]); } else if (rowData[j].getClass() == int.class) { // int類型數值 cell.setCellValue((Integer) rowData[j]); } } } String fullPath = filePath + File.separator + fileName + ".xls";// 含文件名的全路徑
File file = new File(fullPath); if (!file.getParentFile().exists()) { // 如果父目錄不存在,創建父目錄 file.getParentFile().mkdirs(); } if (file.exists()) { // 如果已存在,刪除舊文件 file.delete(); } file = new File(fullPath); file.createNewFile();
FileOutputStream fileOut = new FileOutputStream(file); // 寫出數據到文件 wb.write(fileOut); fileOut.close(); } catch (Exception e) { flag = false; e.printStackTrace(); } return flag; } }