java:利用jexcelapi 建立Excel

來源:互聯網
上載者:User

the project need create the Excel files.i try to use MS Lib in .net but it cost  a lot of  menory.so i get the JexcelApi and POI to create excel file,it  is great.

this page is keep code,nothing else,:>

this is the process Class:

package jExcel;
import jxl.*;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;
import java.io.*;
import java.util.*;
public class Export {

    /**
     * Data Items
     */
    private static List<String[]> items = new ArrayList<String[]>();

    /**
     * Generate Items
     * 
     * @param Address
     * @param Zip
     */
    public static void GenerateItem(String Address, String Zip) {
        items.add(new String[]{Address,Zip});
    }

    /**
     * Export To Excel
     * 
     * @param filepath
     * @throws IOException
     * @throws RowsExceededException
     * @throws WriteException
     */
    public static void ExportToExcel(String filepath) throws IOException,
            RowsExceededException, WriteException {
        File file = new File(filepath);
        //check file exsist
        if (file.exists()) {

        } else {
            file.createNewFile();
        }
        
        //create create a workbook 
        WritableWorkbook book = Workbook.createWorkbook(file);
        //create a sheet 
        WritableSheet sheet = book.createSheet("sheet1", 0);
        //init a new label
        Label label = null;
        
        /**
         * create head line
         */
        //create a new label
        label = new Label(0, 0, "Address");
        //add label into sheet
        sheet.addCell(label);
        //TODO:you can add cell format here like:WritableCellFormat
        label = new Label(1, 0, "Zip");
        sheet.addCell(label);
        //add item cells
        for (int i = 0; i < items.size(); i++) {
            String[] temp = items.get(i);
            for (int s = 0; s < temp.length; s++) {
                label = new Label(s,i + 1, temp[s]);
                sheet.addCell(label);
            }
        }
        //begin to write excel to disk
        book.write();
        //close this steam
        book.close();
    }
}

 

this is the main function class:

 

import java.io.*;
import java.util.*;

import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import jExcel.*;

public class ExApplication {

    static String s = "";
    static BufferedReader input = new BufferedReader(new InputStreamReader(
            System.in));

    static String getFilename() {
        String temp = "";
        File file = new File(".");
        temp = file.getAbsolutePath().substring(0,
                file.getAbsolutePath().lastIndexOf("\\"))
                + "\\";
        Date day = new Date();
        temp += day.toString().replace(" ", "").replace(":", "") + ".xls";
        return temp;
    }

    /**
     * @param args
     * @throws IOException
     * @throws WriteException
     * @throws RowsExceededException
     */
    public static void main(String[] args) throws IOException,
            RowsExceededException, WriteException {
        System.out.println("Please input Address,Zip");
        while (!s.toLowerCase().equals("done")) {
            if (s.toLowerCase().indexOf(",") > -1) {
                String[] temp = s.toLowerCase().split(",");
                if (temp.length == 2) {
                    Export.GenerateItem(temp[0], temp[1]);
                }
            }
            s = input.readLine();
        }
        if (s.toLowerCase().equals("done")) {
            try {
                String name = getFilename();
                Export.ExportToExcel(name);
                System.out.println("Create Complete File :" + name);

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

    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.