0

我正在尝试创建一种方法来填充 excel 表是否已填满,但我不知道当 excel 表已填满时如何实现代码。有人能帮助我吗?这是代码:

我正在使用 API JXL

public void fillData(JTable table, File file) {

    try {

        boolean controladorExcel = false; //Excel controler, if is filled up or not

        Workbook workbookPreenchido = Workbook.getWorkbook(new File(file.getName())); //workbook is filled up

        Sheet sheet = workbookPreenchido.getSheet(0); 

            Cell a1 = sheet.getCell(0, 0);

            String as1 = a1.getContents();

            if(as1 == null){
                controladorExcel = true; //Excel table is blank
            }else{
                controladorExcel = false; //Excel table is filled up
            }


        workbookPreenchido.close();

        if (controladorExcel == true) { //If the excel table is blank
            WritableWorkbook workbookVazio = Workbook.createWorkbook(file); //workbook is blank


            WritableSheet sheet1 = workbookVazio.createSheet("First Sheet", 0);
            TableModel model = table.getModel();

            for (int i = 0; i < model.getColumnCount(); i++) {
                Label column = new Label(i, 0, model.getColumnName(i));
                sheet1.addCell(column);
            }
            int j = 0;
            for (int i = 0; i < model.getRowCount(); i++) {
                for (j = 0; j < model.getColumnCount(); j++) {
                    Label row = new Label(j, i + 1,
                            model.getValueAt(i, j).toString());
                    sheet1.addCell(row);
                }
            }
            workbookVazio.write();
            workbookVazio.close();

        }else{ //If the excel table is filled up

        }          

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
4

2 回答 2

0
package fridayPackage;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;
import javax.swing.table.*;

import jxl.*;
import jxl.write.*;

public class ExcelExporter_UpdatesGood {

    static int x=-2;      ////////Change this to -1 or 0, to leave 

    void fillData(JTable table, File file) {

        try {




            File a = new File("E:\\result.xls");

      ///////////////My New Code    
            if(!a.exists())
            {
                System.out.println("The file doesn't exist.");

                            ////////////////Here it creates a new result.xls

                try {

                    WritableWorkbook workbook1 = Workbook.createWorkbook(file);
                    WritableSheet sheet1 = workbook1.createSheet("First Sheet", 0);
                    TableModel model = table.getModel();

                    for (int i = 0; i < model.getColumnCount(); i++) {                  //////Remove this for loop and Tell what all columns
                        Label column = new Label(i+1, 0, model.getColumnName(i));       //////have to be there.
                        sheet1.addCell(column);
                    }
                    int j = 0;
                    for (int i = 0; i < model.getRowCount(); i++) {
                        for (j = 0; j < model.getColumnCount(); j++) {
                            Label row = new Label(i + 1,j, model.getValueAt(i, j).toString());
                            sheet1.addCell(row);
                        }
                    }
                    workbook1.write();
                    workbook1.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }           

                            ////////////////New File Creation over

            }


            else
            {


     ////////////////My New Code Over       

            Workbook workbook1 = Workbook.getWorkbook(a);

    //        WritableWorkbook workbook1 = Workbook.createWorkbook(file);
            WritableWorkbook copy = Workbook.createWorkbook(file, workbook1);

            WritableSheet sheet2 = copy.getSheet(0); 


            TableModel model = table.getModel();

            for (int i = 0; i < model.getRowCount(); i++) {         //////Remove this for loop and Tell what all columns
                Label row = new Label(i+1,0, "Hello_Column");       //////have to be there.
                sheet2.addCell(row);
            }


            int j = 0;
            for (int i = 0; i < model.getRowCount(); i++) {
                for (j = 1; j < model.getColumnCount(); j++) {
                    Label row = new Label(i + 1  ,j + x + 1 , model.getValueAt(i, j).toString()); ///make it i + 1 - 1
                    sheet2.addCell(row);


                }
            }


            copy.write();
            copy.close();
        }                           //////////////////Closing Else for File exists or not


        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String[][] data = {{"Housewares", "Rs.1275.00"},
            {"Pets", "Rs.125.00"}, {"Electronics", "Rs.2533.00"},
            {"Menswear", "Rs.497.00"}
        };
        String[] headers = {"Field1", "Field2"};

        JFrame frame = new JFrame("JTable to Excel");
        DefaultTableModel model = new DefaultTableModel(data, headers);

        final JTable table = new JTable(model);
        JScrollPane scroll = new JScrollPane(table);

        JButton export = new JButton("Export");
        export.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {

                try {
                    ExcelExporter_UpdatesGood exp = new ExcelExporter_UpdatesGood();

                    File a = new File ("E:\\result.xls");

                    x=x+1;              //////Incrementing x here

                    exp.fillData(table, a);


                    JOptionPane.showMessageDialog(null, "Data saved at " +
                            "'E: \\ result.xls' successfully", "Message",
                            JOptionPane.INFORMATION_MESSAGE);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        frame.getContentPane().add("Center", scroll);
        frame.getContentPane().add("South", export);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setAlwaysOnTop(true);
    }
}
于 2014-03-28T18:59:59.430 回答
0
package JustTry;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;
import javax.swing.table.*;

import jxl.*;
import jxl.write.*;

public class dexceleporte {

    void fillData(JTable table, File file) {

        try {

            WritableWorkbook workbook1 = Workbook.createWorkbook(file);
            WritableSheet sheet1 = workbook1.createSheet("First Sheet", 0);
            TableModel model = table.getModel();

            for (int i = 0; i < model.getColumnCount(); i++) {
                Label column = new Label(i, 0, model.getColumnName(i));
                sheet1.addCell(column);
            }
            int j = 0;
            for (int i = 0; i < model.getRowCount(); i++) {
                for (j = 0; j < model.getColumnCount(); j++) {
                    Label row = new Label(j, i + 1,
                            model.getValueAt(i, j).toString());
                    sheet1.addCell(row);
                }
            }
            workbook1.write();
            workbook1.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String[][] data = {{"Housewares", "Rs.1275.00"},
            {"Pets", "Rs.125.00"}, {"Electronics", "Rs.2533.00"},
            {"Menswear", "Rs.497.00"}
        };
        String[] headers = {"Department", "Daily Revenue"};

        JFrame frame = new JFrame("JTable to Excel");
        DefaultTableModel model = new DefaultTableModel(data, headers);
        final JTable table = new JTable(model);
        JScrollPane scroll = new JScrollPane(table);

        JButton export = new JButton("Export");
        export.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {

                try {
                    dexceleporte exp = new dexceleporte();
                    exp.fillData(table, new File("E:\\result.xls"));
                    JOptionPane.showMessageDialog(null, "Data saved at " +
                            "': \\ result.xls' successfully", "Message",
                            JOptionPane.INFORMATION_MESSAGE);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        frame.getContentPane().add("Center", scroll);
        frame.getContentPane().add("South", export);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setAlwaysOnTop(true);

    }
}
于 2014-03-25T15:08:40.427 回答