com.xxx.common.excel.filereader;
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.ArrayList;
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 {
public static void main(String[] args) { //Generating 1 million pieces of data takes a very short time List<Object[]> rows=new ArrayList<Object[]>(); Long start=System.currentTimeMillis(); for(int i=0;i<1000000;i + + ){ rows.add(new Object[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}); } Long end=System.currentTimeMillis(); System.out.println("Time consuming ----- " + (end-start)/1000); String filePath="/Users/wangyonglin/Desktop"; String fileName="test1.txt"; CreateFileUtil.createTxtFile(rows,filePath,fileName); Long kend=System.currentTimeMillis(); System.out.println("The final end takes time, inserting 1 million data, --- end"); System.out.println((kend-end)/1000); } /** * Generate .TXT format files with almost unlimited number of lines */ public static boolean createTxtFile(List<Object[]> rows, String filePath, String fileName) { // Whether the mark file generation is successful boolean flag = true; try { // Full path including file name String fullPath = filePath + File.separator + fileName + ".txt"; File file = new File(fullPath); if (file.exists()) { // If it already exists, delete the old file file.delete(); } file = new File(fullPath); file.createNewFile(); //Format floating point data NumberFormat formatter = NumberFormat.getNumberInstance(); formatter.setMaximumFractionDigits(10); //Set the maximum decimal place to 10 //Format date data SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //Loop through each line of output PrintWriter pfp = new PrintWriter(file, "UTF-8"); //Set the encoding of the output file to utf-8 for (Object[] rowData : rows) { StringBuffer thisLine = new StringBuffer(""); for (int i = 0; i < rowData.length; i + + ) { Object obj = rowData[i]; // current field //Format data String field = ""; if (null != obj) { if (obj.getClass() == String.class) { // if it is a string field = (String) obj; } else if (obj.getClass() == Double.class || obj.getClass() == Float.class) { // If it is floating point type field = formatter.format(obj); // Format floating point numbers so that they are not output in scientific notation } else if (obj.getClass() == Integer.class || obj.getClass() == Long.class || obj.getClass() == Short.class || obj.getClass() == Byte.class) { // If it is an integer field + = obj; } else if (obj.getClass() == Date.class) { // If it is date type field = sdf.format(obj); } } else { field = " "; // Give a space as a placeholder when null } // Splice all fields into one row of data, separated by tab key if (i < rowData.length - 1) { // Not the last element thisLine.append(field).append("\t"); } else { // is the last element thisLine.append(field); } } pfp.print(thisLine.toString() + "\ "); } pfp.close(); } catch (Exception e) { flag = false; e.printStackTrace(); } return flag; } /** * Generate .csv format files with almost unlimited number of lines */ public static boolean createCsvFile(List<Object[]> rows, String filePath, String fileName) { // Whether the mark file generation is successful boolean flag = true; //File output stream BufferedWriter fileOutputStream = null; try { // Full path including file name String fullPath = filePath + File.separator + fileName + ".csv"; File file = new File(fullPath); if (!file.getParentFile().exists()) { // If the parent directory does not exist, create the parent directory file.getParentFile().mkdirs(); } if (file.exists()) { // If it already exists, delete the old file file.delete(); } file = new File(fullPath); file.createNewFile(); //Format floating point data NumberFormat formatter = NumberFormat.getNumberInstance(); formatter.setMaximumFractionDigits(10); //Set the maximum decimal place to 10 //Format date data SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); // Instantiate the file output stream fileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "GB2312"), 1024); //Loop through each line of output Iterator<Object[]> iterator = rows.iterator(); while (ite.hasNext()) { Object[] rowData = (Object[]) ite.next(); for (int i = 0; i < rowData.length; i + + ) { Object obj = rowData[i]; // current field //Format data String field = ""; if (null != obj) { if (obj.getClass() == String.class) { // if it is a string field = (String) obj; } else if (obj.getClass() == Double.class || obj.getClass() == Float.class) { // If it is floating point type field = formatter.format(obj); // Format floating point numbers so that they are not output in scientific notation } else if (obj.getClass() == Integer.class || obj.getClass() == Long.class || obj.getClass() == Short.class || obj.getClass() == Byte.class) { // If it is an integer field + = obj; } else if (obj.getClass() == Date.class) { // If it is date type field = sdf.format(obj); } } else { field = " "; // Give a space as a placeholder when null } // Splice all fields into one row of data if (i < rowData.length - 1) { // Not the last element fileOutputStream.write(""" + field + """ + ","); } else { // is the last element fileOutputStream.write(""" + field + """); } } // Create a new row 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; } /** * Generate .xls format file, the upper limit of a single page: version 03 is 65536 lines, version 07 is 1048576 lines, version 10 is unknown */ public static boolean createXlsFile(List<Object[]> rows, String filePath, String fileName) { // Whether the mark file generation is successful boolean flag = true; try { //Create a webbook corresponding to an Excel file XSSFWorkbook wb = new XSSFWorkbook(); //Add a sheet in the webbook, corresponding to the sheet in the Excel file XSSFSheet sheet = wb.createSheet(fileName); //Loop through each line of output for (int i = 0; i < rows.size(); i + + ) { Object[] rowData = rows.get(i); //Data for each row XSSFRow row = sheet.createRow(i); for (int j = 0; j < rowData.length; j + + ) { XSSFCell cell = row.createCell(j); // Assume there are only three types of data if (rowData[j].getClass() == String.class) { // String type value cell.setCellValue((String) rowData[j]); } else if (rowData[j].getClass() == double.class) { // double type value cell.setCellValue((Double) rowData[j]); } else if (rowData[j].getClass() == int.class) { // int type value cell.setCellValue((Integer) rowData[j]); } } } String fullPath = filePath + File.separator + fileName + ".xls"; // Full path including file name File file = new File(fullPath); File file = new File(fullPath); if (!file.getParentFile().exists()) { // If the parent directory does not exist, create the parent directory file.getParentFile().mkdirs(); } if (file.exists()) { // If it already exists, delete the old file file.delete(); } file.createNewFile(); FileOutputStream fileOut = new FileOutputStream(file); // Write data to file wb.write(fileOut); fileOut.close(); } catch (Exception e) { flag = false; e.printStackTrace(); } return flag;