JAVA代码无法通过声明的List数组写入Excel

时间:2018-03-27 09:00:47

标签: java excel

我的要求是从csv文件中读取并使用单个字段写入每列中的excel文件。我成功地从CSV文件中读取。但是从List数组读取并写入excel文件时出错。我附加了代码片段,突出显示错误。让我知道这个问题,如果可能的话,请发送修复程序和任何相同功能的示例代码。

更新的代码包testmaven.testmv;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

import javax.swing.text.html.HTMLDocument.Iterator;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.poi.hpsf.HPSFException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class CSVReader {
    private static class Country {
        private String ip_address;
        private String full_ip_address;
        private String resource_name;
        private String serial_number;
        private String status;
        private String inactive;
        private String organistation;
        private String system_name;
        private String system_org;
        private String system_bus_crtcl;
        private String vlan_name;
        private String vlan_desc ;
        private String vlan_contact;
        private String class_1;
        private String family;
        private String assignee_zid;
        private String contact_applied;
        private String assignee_type;
        private String assignee_email_address;
        private String assignee_org_name;
        private String assignee_full_name;

        public Country(String ip_address, String full_ip_address, String resource_name,String serial_number, String status,String inactive,String organistation,String system_name,String system_org,String system_bus_crtcl,String vlan_name,String vlan_desc, String vlan_contact,String class_1,String family,String assignee_zid, String contact_applied,String assignee_type,String assignee_email_address,String assignee_org_name,String assignee_full_name) {
            this.ip_address = ip_address;
            this.full_ip_address = full_ip_address;
            this.resource_name = resource_name;
            this.serial_number = serial_number;
            this.status = status;
            this.inactive = inactive;
            this.organistation = organistation;
            this.system_name = system_name;
            this.system_org = system_org;
            this.system_bus_crtcl = system_bus_crtcl;
            this.vlan_name = vlan_name;
            this.vlan_desc = vlan_desc;
            this.vlan_contact = vlan_contact;
            this.class_1  = class_1 ;
            this.family = family;
            this.assignee_zid = assignee_zid;
            this.contact_applied = contact_applied;
            this.assignee_type = assignee_type;
            this.assignee_email_address = assignee_email_address;
            this.assignee_org_name = assignee_org_name;
            this.assignee_full_name = assignee_full_name;











        }


        /**
         * @return the ip_address
         */
        public String getIp_address() {
            return ip_address;
        }


        /**
         * @param ip_address the ip_address to set
         */
        public void setIp_address(String ip_address) {
            this.ip_address = ip_address;
        }


        /**
         * @return the full_ip_address
         */
        public String getFull_ip_address() {
            return full_ip_address;
        }


        /**
         * @param full_ip_address the full_ip_address to set
         */
        public void setFull_ip_address(String full_ip_address) {
            this.full_ip_address = full_ip_address;
        }


        /**
         * @return the resource_name
         */
        public String getResource_name() {
            return resource_name;
        }


        /**
         * @param resource_name the resource_name to set
         */
        public void setResource_name(String resource_name) {
            this.resource_name = resource_name;
        }


        /**
         * @return the serial_number
         */
        public String getSerial_number() {
            return serial_number;
        }


        /**
         * @param serial_number the serial_number to set
         */
        public void setSerial_number(String serial_number) {
            this.serial_number = serial_number;
        }


        /**
         * @return the status
         */
        public String getStatus() {
            return status;
        }


        /**
         * @param status the status to set
         */
        public void setStatus(String status) {
            this.status = status;
        }


        /**
         * @return the inactive
         */
        public String getInactive() {
            return inactive;
        }


        /**
         * @param inactive the inactive to set
         */
        public void setInactive(String inactive) {
            this.inactive = inactive;
        }


        /**
         * @return the organistation
         */
        public String getOrganistation() {
            return organistation;
        }


        /**
         * @param organistation the organistation to set
         */
        public void setOrganistation(String organistation) {
            this.organistation = organistation;
        }


        /**
         * @return the system_name
         */
        public String getSystem_name() {
            return system_name;
        }


        /**
         * @param system_name the system_name to set
         */
        public void setSystem_name(String system_name) {
            this.system_name = system_name;
        }


        /**
         * @return the system_org
         */
        public String getSystem_org() {
            return system_org;
        }


        /**
         * @param system_org the system_org to set
         */
        public void setSystem_org(String system_org) {
            this.system_org = system_org;
        }


        /**
         * @return the system_bus_crtcl
         */
        public String getSystem_bus_crtcl() {
            return system_bus_crtcl;
        }


        /**
         * @param system_bus_crtcl the system_bus_crtcl to set
         */
        public void setSystem_bus_crtcl(String system_bus_crtcl) {
            this.system_bus_crtcl = system_bus_crtcl;
        }


        /**
         * @return the vlan_name
         */
        public String getVlan_name() {
            return vlan_name;
        }


        /**
         * @param vlan_name the vlan_name to set
         */
        public void setVlan_name(String vlan_name) {
            this.vlan_name = vlan_name;
        }


        /**
         * @return the vlan_desc
         */
        public String getVlan_desc() {
            return vlan_desc;
        }


        /**
         * @param vlan_desc the vlan_desc to set
         */
        public void setVlan_desc(String vlan_desc) {
            this.vlan_desc = vlan_desc;
        }


        /**
         * @return the vlan_contact
         */
        public String getVlan_contact() {
            return vlan_contact;
        }


        /**
         * @param vlan_contact the vlan_contact to set
         */
        public void setVlan_contact(String vlan_contact) {
            this.vlan_contact = vlan_contact;
        }


        /**
         * @return the class_1
         */
        public String getClass_1() {
            return class_1;
        }


        /**
         * @param class_1 the class_1 to set
         */
        public void setClass_1(String class_1) {
            this.class_1 = class_1;
        }


        /**
         * @return the family
         */
        public String getFamily() {
            return family;
        }


        /**
         * @param family the family to set
         */
        public void setFamily(String family) {
            this.family = family;
        }


        /**
         * @return the assignee_zid
         */
        public String getAssignee_zid() {
            return assignee_zid;
        }


        /**
         * @param assignee_zid the assignee_zid to set
         */
        public void setAssignee_zid(String assignee_zid) {
            this.assignee_zid = assignee_zid;
        }


        /**
         * @return the contact_applied
         */
        public String getContact_applied() {
            return contact_applied;
        }


        /**
         * @param contact_applied the contact_applied to set
         */
        public void setContact_applied(String contact_applied) {
            this.contact_applied = contact_applied;
        }


        /**
         * @return the assignee_type
         */
        public String getAssignee_type() {
            return assignee_type;
        }


        /**
         * @param assignee_type the assignee_type to set
         */
        public void setAssignee_type(String assignee_type) {
            this.assignee_type = assignee_type;
        }


        /**
         * @return the assignee_email_address
         */
        public String getAssignee_email_address() {
            return assignee_email_address;
        }


        /**
         * @param assignee_email_address the assignee_email_address to set
         */
        public void setAssignee_email_address(String assignee_email_address) {
            this.assignee_email_address = assignee_email_address;
        }


        /**
         * @return the assignee_org_name
         */
        public String getAssignee_org_name() {
            return assignee_org_name;
        }


        /**
         * @param assignee_org_name the assignee_org_name to set
         */
        public void setAssignee_org_name(String assignee_org_name) {
            this.assignee_org_name = assignee_org_name;
        }


        /**
         * @return the assignee_full_name
         */
        public String getAssignee_full_name() {
            return assignee_full_name;
        }


        /**
         * @param assignee_full_name the assignee_full_name to set
         */
        public void setAssignee_full_name(String assignee_full_name) {
            this.assignee_full_name = assignee_full_name;
        }


        @Override
        public String toString() { // return "Country [name=" + name +
                                    // ", capital=" + capital + ", currency=" +
                                    // currency + "]";
            return this.ip_address + this.full_ip_address + this.resource_name + this.serial_number;
        }
    }

    public static <E> void main(String args[]) throws FileNotFoundException,
            IOException {
        System.out
                .println("Reading from CSV file using BufferedReader and String Split");

        // List nation11 = readCSV();
        ArrayList<Country> nation11 = (ArrayList<CSVReader.Country>) readCSV();
        //System.out.println(nation11);

        int rowCount = 0;
        String excelFilePath = "D://abc.xls";
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        Row row = sheet.createRow(++rowCount);
        // writeBook(aBook, row);
        Cell cell = row.createCell(0);
        cell.setCellValue("ip_address");

        cell = row.createCell(1);
        cell.setCellValue("full_ip_address");

        cell = row.createCell(2);
        cell.setCellValue("resource_name");

        cell = row.createCell(3);
        cell.setCellValue("serial_number");

        cell = row.createCell(4);
        cell.setCellValue("status");

        cell = row.createCell(5);
        cell.setCellValue("inactive");

        cell = row.createCell(6);
        cell.setCellValue("organistation");

        cell = row.createCell(7);
        cell.setCellValue("system_name");

        cell = row.createCell(8);
        cell.setCellValue("system_org");

        cell = row.createCell(9);
        cell.setCellValue("system_bus_crtcl");

        cell = row.createCell(10);
        cell.setCellValue("vlan_name");

        cell = row.createCell(11);
        cell.setCellValue("vlan_desc");

        cell = row.createCell(12);
        cell.setCellValue("vlan_contact");

        cell = row.createCell(13);
        cell.setCellValue("class_1");

        cell = row.createCell(14);
        cell.setCellValue("family");

        cell = row.createCell(15);
        cell.setCellValue("assignee_zid ");

        cell = row.createCell(16);
        cell.setCellValue("contact_applied");

        cell = row.createCell(17);
        cell.setCellValue("assignee_type");

        cell = row.createCell(18);
        cell.setCellValue("assignee_email_address");

        cell = row.createCell(19);
        cell.setCellValue("assignee_org_name");

        cell = row.createCell(20);
        cell.setCellValue("assignee_full_name");

        for (Country country : nation11) {

            row = sheet.createRow(++rowCount);
            // writeBook(aBook, row);
            cell = row.createCell(0);
            cell.setCellValue(country.ip_address);

            cell = row.createCell(1);
            cell.setCellValue(country.full_ip_address);

            cell = row.createCell(2);
            cell.setCellValue(country.resource_name);

            cell = row.createCell(3);
            cell.setCellValue(country.serial_number);

            cell = row.createCell(4);
            cell.setCellValue(country.status);

            cell = row.createCell(5);
            cell.setCellValue(country.inactive);

            cell = row.createCell(6);
            cell.setCellValue(country.organistation);

            cell = row.createCell(7);
            cell.setCellValue(country.system_name);

            cell = row.createCell(8);
            cell.setCellValue(country.system_org);

            cell = row.createCell(9);
            cell.setCellValue(country.system_bus_crtcl);

            cell = row.createCell(10);
            cell.setCellValue(country.vlan_name);

            cell = row.createCell(11);
            cell.setCellValue(country.vlan_desc);

            cell = row.createCell(12);
            cell.setCellValue(country.vlan_contact );

            cell = row.createCell(13);
            cell.setCellValue(country.class_1);

            cell = row.createCell(14);
            cell.setCellValue(country.family);

            cell = row.createCell(15);
            cell.setCellValue(country.assignee_zid);

            cell = row.createCell(16);
            cell.setCellValue(country.contact_applied);

            cell = row.createCell(17);
            cell.setCellValue(country.assignee_type);

            cell = row.createCell(18);
            cell.setCellValue(country.assignee_email_address);

            cell = row.createCell(19);
            cell.setCellValue(country.assignee_org_name);

            cell = row.createCell(20);
            cell.setCellValue(country.assignee_full_name);


        }

    //  ArrayList<Country> nation12 = nation11.replaceAll("\"","");     
        FileOutputStream outputStream = new FileOutputStream(excelFilePath);
    //  String abc = ((String) outputStream).replace("\""," ");

        workbook.write(outputStream);
    /*  FileWriter writer1 = null;
        writer1 = new FileWriter("D://abc.xls");*/

        /*BufferedReader br2 = new BufferedReader(new FileReader("D://abc.xls"));
        String newLine = " ";
        while ((newLine = br2.readLine()) != null && !newLine.isEmpty())

        {
            System.out.println(newLine);
            newLine = newLine.replace("\"", "");
    }*/
    }

    // }
    // parseCSV(); }

    public static List readCSV() throws FileNotFoundException, IOException {
        List countries = new ArrayList<>();
        FileWriter writer = null;
        String line = " ";
        String oldContent = " ";
        String newContent = " ";
        // writer = new FileWriter("D://data1.csv");
        BufferedReader br = new BufferedReader(new FileReader("D://data1.csv"));

        // System.out.println(br.readLine());
        while ((line = br.readLine()) != null && !line.isEmpty())

        {
        //  System.out.println(line);
            line = line.replace("|", ",");
            oldContent = oldContent + line + System.lineSeparator();

        //  System.out.println("after change " + line);
        //  System.out.println("after change OLD" + oldContent);
            // line.replaceAll("|",",");

        }
        //System.out.println("here am I " + oldContent);
        // newContent = oldContent.replaceAll("|",",");
        writer = new FileWriter("D://data1.csv");
        writer.write(oldContent);
        writer.close();
        // line= oldContent;
        // System.out.println(newContent);
        BufferedReader br1 = new BufferedReader(new FileReader("D://data1.csv"));
        line = " ";
        line = br1.readLine(); // Reading header, Ignoring
        //System.out.println("here am I again " + line);
        while ((line = br1.readLine()) != null && !line.isEmpty()) {
            String[] fields = line.split(",");
        //  System.out.println("fileds : " + fields[0]);

            String ip_address = fields[0];
        //  System.out.println("before removing double quotes : " + ip_address);

             ip_address = ip_address.replaceAll("\"", " ");
        //  System.out.println("after removing double quotes : " + ip_address);
            String full_ip_address = fields[1];
            full_ip_address = full_ip_address.replaceAll("\"", " ");
            String resource_name = fields[2];
            resource_name = resource_name.replaceAll("\"", " ");
            String serial_number = fields[3];
            serial_number = serial_number.replaceAll("\"", " "); 
            String status = fields[4];
            status = status.replaceAll("\"", " ");
            String inactive = fields[5];
            inactive = inactive.replaceAll("\"", " ");
            String organistation = fields[6];
            organistation = organistation.replaceAll("\"", " ");
            String system_name = fields[7];
            system_name = system_name.replaceAll("\"", " "); 
            String system_org = fields[8];
            system_org  = system_org.replaceAll("\"", " ");
            String system_bus_crtcl = fields[9];
            system_bus_crtcl = system_bus_crtcl.replaceAll("\"", " "); 
            String vlan_name = fields[10];
            vlan_name = vlan_name.replaceAll("\"", " ");
            String vlan_desc = fields[11];
            vlan_desc = vlan_desc.replaceAll("\"", " ");
            String vlan_contact = fields[12];
            vlan_contact =  vlan_contact.replaceAll("\"", " "); 
            String class_1 = fields[13];
            class_1 = class_1.replaceAll("\"", " ");
            String family = fields[14];
            family = family.replaceAll("\"", " ");
            String assignee_zid = fields[15];
            assignee_zid = assignee_zid.replaceAll("\"", " ");
            String contact_applied = fields[16];
            contact_applied = contact_applied.replaceAll("\"", " ");
            String assignee_type = fields[17];
            assignee_type = assignee_type.replaceAll("\"", " ");
            String assignee_email_address = fields[18];
            assignee_email_address = assignee_email_address.replaceAll("\"", " ");
            String assignee_org_name = fields[19];
            assignee_org_name = assignee_org_name.replaceAll("\"", " ");
            String assignee_full_name = fields[20];
            assignee_full_name = assignee_full_name.replaceAll("\"", " ");

            Country nation = new Country(ip_address, full_ip_address, resource_name, serial_number,status,inactive,organistation,system_name,system_org,system_bus_crtcl,vlan_name,vlan_desc,vlan_contact,class_1,family,assignee_zid,contact_applied,assignee_type,assignee_email_address,assignee_org_name,assignee_full_name );
            //System.out.println("am here : " + fields[1]);
            countries.add(nation);
        }
        br.close();

        return countries;
    }
}

当我输入45k数据时,代码不会产生任何结果而不会抛出任何错误。你能建议吗。

1 个答案:

答案 0 :(得分:0)

String excelFilePath = "D://abc.xlsx";

for.xlsx文件你应该使用

 XSSFWorkbook  wb = new XSSFWorkbook();  API

或将扩展名更改为

String excelFilePath = "D://abc.xls";

&安培;把数据放在xls文件

for (Country country : nation11) {
            Row row = sheet.createRow(++rowCount);
            // writeBook(aBook, row);
            Cell cell = row.createCell(0);
            cell.setCellValue(country.name);

            cell = row.createCell(1);
            cell.setCellValue(country.capital);

            cell = row.createCell(2);
            cell.setCellValue(country.currency);
        }

它有效