用java写出excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3454975/
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-14 00:56:18  来源:igfitidea点击:

writing to excel in java

javaexcelexcel-2007

提问by JJunior

Can someone point me in the right direction for writing to an excel file in java?? I am not understanding the links I found online. Could you just send me a link or anything which I could follow through??

有人可以指出我在java中写入excel文件的正确方向吗??我不明白我在网上找到的链接。你能不能给我一个链接或任何我可以跟进的东西??

Thank you, J

谢谢你,J

采纳答案by Nikita Rybak

Not to be banal, but Apache POI can do it. You can find some code examples here:
http://poi.apache.org/spreadsheet/examples.html

不是平庸,但 Apache POI 可以做到。您可以在这里找到一些代码示例:http:
//poi.apache.org/spreadsheet/examples.html

回答by matt b

Another alternative to Apache POI is the JExcelAPI, which (IMO) has an easier to use API. Some examples:

Apache POI 的另一种替代方法是JExcelAPI,它 (IMO) 具有更易于使用的 API。一些例子

WritableWorkbook workbook = Workbook.createWorkbook(new File("output.xls"));

WritableSheet sheet = workbook.createSheet("First Sheet", 0);

Label label = new Label(0, 2, "A label record"); 
sheet.addCell(label); 

Number number = new Number(3, 4, 3.1459); 
sheet.addCell(number);

回答by FloppyDisk

I've used Apache's POI Librarywhen I've had to write to excel files from Java. I found it rather straight forward once you get the hang of it. Java World has a good tutorialabout starting out using POI that I found very helpful.

当我不得不从 Java 写入 Excel 文件时,我使用了Apache 的 POI 库。一旦你掌握了它,我发现它相当直接。Java World 有一个关于开始使用 POI的很好的教程,我发现它很有帮助。

回答by user2663609

Here i'm going to give sample example,how to write excel;use this in pom.xml

在这里我将给出示例示例,如何编写 excel;在 pom.xml 中使用它

<dependency>
    <groupId>net.sf.jxls</groupId>
    <artifactId>jxls-core</artifactId>
    <version>0.9</version>
</dependency>

Model class:

模型类:

public class Products {    
    private String productCode1;    
    private String productCode2;    
    private String productCode3;   

    constructors,setters and getters    
}

main class:

主类:

public class WriteExcel {

    public void writeExcel() {

        Collection staff = new HashSet();    

        staff.add(new Products("101R15ss0100", "PALss Kids 1.5 A360 ", "321"));    
        staff.add(new Products("101R1ss50100", "PAL sKids 1.5 A360 ", "236"));    

        Map beans = new HashMap();    

        beans.put("products", staff);    
        XLSTransformer transformer = new XLSTransformer();    

        try {    
            transformer.transformXLS(templateFileName, beans, destFileName);   

            System.out.println("Completed!!");    

        } catch (ParsePropertyException e) {    
            System.out.println("In ParsePropertyException");    
            e.printStackTrace();    
        } catch (IOException e) {    
            System.out.println("In IOException");    
            e.printStackTrace();    
        }
    }

    public static void main(String[] args) {    
        WriteExcel writeexcel = new WriteExcel();    
        writeexcel.writeExcel();    
    }    

}    

回答by satender

public class ExcelUtils {

    private static XSSFSheet ExcelWSheet;
    private static XSSFWorkbook ExcelWBook;
    private static XSSFCell Cell;
    private static XSSFRow Row;
    File fileName = new File("C:\Users\satekuma\Pro\Fund.xlsx");

    public void setExcelFile(File Path, String SheetName) throws Exception {
        try {
            FileInputStream ExcelFile = new FileInputStream(Path);
            ExcelWBook = new XSSFWorkbook(ExcelFile);
            ExcelWSheet = ExcelWBook.getSheet(SheetName);
        } catch (Exception e) {
            throw (e);
        }
    }


    public static String getCellData(int RowNum, int ColNum) throws      Exception {
        try {
            Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
            String CellData = Cell.getStringCellValue();
            return CellData;
        } catch (Exception e) {
            return "";
        }
    }

    public static void setCellData(String Result, int RowNum, int ColNum, File Path) throws Exception {
        try {
            Row = ExcelWSheet.createRow(RowNum - 1);
            Cell = Row.createCell(ColNum - 1);
            Cell.setCellValue(Result);
            FileOutputStream fileOut = new FileOutputStream(Path);
            ExcelWBook.write(fileOut);
            fileOut.flush();
            fileOut.close();
        } catch (Exception e) {
            throw (e);
        }
    }
}

回答by Rahul Raina

Here is the basic code to write data in excel file (.xls).

这是在excel文件(.xls)中写入数据的基本代码。

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

public class Export2Excel {

    public static void main(String[] args) {
        try {
            //create .xls and create a worksheet.
            FileOutputStream fos = new FileOutputStream("D:\data2excel.xls");
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet worksheet = workbook.createSheet("My Worksheet");

            //Create ROW-1
            HSSFRow row1 = worksheet.createRow((short) 0);

            //Create COL-A from ROW-1 and set data
            HSSFCell cellA1 = row1.createCell((short) 0);
            cellA1.setCellValue("Sno");
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellA1.setCellStyle(cellStyle);

            //Create COL-B from row-1 and set data
            HSSFCell cellB1 = row1.createCell((short) 1);
            cellB1.setCellValue("Name");
            cellStyle = workbook.createCellStyle();
            cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellB1.setCellStyle(cellStyle);

            //Create COL-C from row-1 and set data
            HSSFCell cellC1 = row1.createCell((short) 2);
            cellC1.setCellValue(true);

            //Create COL-D from row-1 and set data
            HSSFCell cellD1 = row1.createCell((short) 3);
            cellD1.setCellValue(new Date());
            cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(HSSFDataFormat
                    .getBuiltinFormat("m/d/yy h:mm"));
            cellD1.setCellStyle(cellStyle);

            //Save the workbook in .xls file
            workbook.write(fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

NOTE:You need to download jar library from the link:http://www.java2s.com/Code/JarDownload/poi/poi-3.9.jar.zip

注意:您需要从以下链接下载 jar 库:http : //www.java2s.com/Code/JarDownload/poi/poi-3.9.jar.zip