Java 如何使用 JXL 创建新的 Excel 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/150646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:57:38  来源:igfitidea点击:

How Do I Create a New Excel File Using JXL?

javajxl

提问by Aaron

I'm trying to create a new Excel file using jxl, but am having a hard time finding examples in their API documentation and online.

我正在尝试使用 jxl 创建一个新的 Excel 文件,但是很难在他们的 API 文档和在线找到示例。

采纳答案by Aaron

After messing around awhile longer I finally found something that worked and saw there still wasn't a solution posted here yet, so here's what I found:

折腾了一段时间后,我终于找到了一些有用的东西,但看到这里仍然没有发布解决方案,所以这是我发现的:

try {
    String fileName = "file.xls";
    WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
    workbook.createSheet("Sheet1", 0);
    workbook.createSheet("Sheet2", 1);
    workbook.createSheet("Sheet3", 2);
    workbook.write();
    workbook.close();
} catch (WriteException e) {

}

回答by kolrie

Not sure if you need to stick with JXL, but the best library for handling Excel files is Apache's POI HSSF.

不确定您是否需要坚持使用 JXL,但处理 Excel 文件的最佳库是Apache 的 POI HSSF

I think there are plenty of examples on the website I provided, but if you need further assistence, let me know. I may have a few examples laying around.

我认为我提供的网站上有很多示例,但是如果您需要进一步的帮助,请告诉我。我可能有几个例子。

Just out of curiosity, POI stands for Poor Obfuscation Interface and HSSF is Horrible SpreadSheet Format. You see how much Apache loves Microsoft Office formats :-)

出于好奇,POI 代表Poor Obfuscation Interface,而HSSF 代表Horrible SpreadSheet 格式。您会看到 Apache 有多喜欢 Microsoft Office 格式:-)

回答by Zaheer Astori

First of all you need to put Jxl Api into your java directory , download JXL api from http://www.andykhan.com/extract it,copy jxl and paste like C:\Program Files\Java\jre7\lib\ext.

首先你需要把Jxl Api放到你的java目录下,从http://www.andykhan.com/下载JXL api解压,复制jxl并像C:\Program Files\Java\jre7\lib\ext一样粘贴。

try {
    String fileName = "file.xls";
    WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
    WritableSheet writablesheet1 = workbook.createSheet("Sheet1", 0);
    WritableSheet writablesheet2 = workbook.createSheet("Sheet2", 1);
    WritableSheet writablesheet3 = workbook.createSheet("Sheet3", 2);
    Label label1 = new Label("Emp_Name");
    Label label2 = new Label("Emp_FName");
    Label label3 = new Label("Emp_Salary");
    writablesheet1.addCell(label1);
    writablesheet2.addCell(label2);
    writablesheet3.addCell(label3);
    workbook.write();
    workbook.close();
} catch (WriteException e) {

}

回答by Almir Campos

I know that it's a very old question. However, I think I can contribute with an example that also adds the cell values:

我知道这是一个非常古老的问题。但是,我想我可以提供一个还添加单元格值的示例:

/**
 *
 * @author Almir Campos
 */
public class Write01
{
    public void test01() throws IOException, WriteException
    {
        // Initial settings
        File file = new File( "c:/tmp/genexcel.xls" );
        WorkbookSettings wbs = new WorkbookSettings();
        wbs.setLocale( new Locale( "en", "EN" ) );
        // Creates the workbook
        WritableWorkbook wwb = Workbook.createWorkbook( file, wbs );
        // Creates the sheet inside the workbook
        wwb.createSheet( "Report", 0 );
        // Makes the sheet writable
        WritableSheet ws = wwb.getSheet( 0 );
        // Creates a cell inside the sheet
        //CellView cv = new CellView();
        Number n;
        Label l;
        Formula f;
        for ( int i = 0; i < 10; i++ )
        {
            // A
            n = new Number( 0, i, i );
            ws.addCell( n );
            // B
            l = new Label( 1, i, "by" );
            ws.addCell( l );
            // C
            n = new Number( 2, i, i + 1 );
            ws.addCell( n );
            // D
            l = new Label( 3, i, "is" );
            ws.addCell( l );
            // E
            f = new Formula(4, i, "A" + (i+1) + "*C" + (i+1) );
            ws.addCell( f );
        }
        wwb.write();
        wwb.close();
    }
}

回答by Kavos Khajavi

public void exportToExcel() {
    final String fileName = "TodoList2.xls";

    //Saving file in external storage
    File sdCard = Environment.getExternalStorageDirectory();
    File directory = new File(sdCard.getAbsolutePath() + "/javatechig.todo");

    //create directory if not exist
    if(!directory.isDirectory()){
        directory.mkdirs();
    }

    //file path
    File file = new File(directory, fileName);

    WorkbookSettings wbSettings = new WorkbookSettings();
    wbSettings.setLocale(new Locale("en", "EN"));
    WritableWorkbook workbook;


    try {
        workbook = Workbook.createWorkbook(file, wbSettings);
        //Excel sheet name. 0 represents first sheet
        WritableSheet sheet = workbook.createSheet("MyShoppingList", 0);



        Cursor  cursor = mydb.rawQuery("select * from Contact", null);

        try {
            sheet.addCell(new Label(0, 0, "id")); // column and row
            sheet.addCell(new Label(1, 0, "name"));
            sheet.addCell(new Label(2,0,"ff "));
            sheet.addCell(new Label(3,0,"uu"));
            if (cursor.moveToFirst()) {
                do {
                    String title =cursor.getString(0) ;
                    String desc = cursor.getString(1);
                    String name=cursor.getString(2);
                    String family=cursor.getString(3);

                    int i = cursor.getPosition() + 1;
                    sheet.addCell(new Label(0, i, title));
                    sheet.addCell(new Label(1, i, desc));
                    sheet.addCell(new Label(2,i,name));
                    sheet.addCell(new Label(3,i,family));
                } while (cursor.moveToNext());
            }
            //closing cursor
            cursor.close();
        } catch (RowsExceededException e) {
            e.printStackTrace();
        } catch (WriteException e) {
            e.printStackTrace();
        }
        workbook.write();
        try {
            workbook.close();
        } catch (WriteException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}