使用Java向Excel写入数据

9

有人能指导我如何在Java中写入Excel文件吗?我不理解我在网上找到的链接。你能给我发送一个链接或者任何可以让我跟进的东西吗?

谢谢, J


你在说哪些链接?有很多不同的方法可以从Java写入Excel文件。你是否遇到了特定的API或库的问题? - FloppyDisk
可能是JSP生成可下载的Excel电子表格(XLS)的重复问题。 - Matt Ball
GemBox.Spreadsheet for Java非常快速,API易于使用,这里有一个写作示例 - bellpatricia
7个回答

13

除了Apache POI之外,另一种选择是JExcelAPI,它(在我看来)具有更易于使用的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);

非常棒的API。比Apache POI容易得多。谢谢! - FAtBalloon

7


2

我将为您提供一个示例,展示如何编写Excel;在pom.xml中使用此示例。

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

模型类:

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

    constructors,setters and getters    
}

主类:

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();    
    }    

}    

1
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);
        }
    }
}

1
这里是编写在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();
        }

    }

}

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


0

在Java中写入Excel文件时,我使用Apache的POI库。一旦你掌握了它,我发现它相当简单易懂。Java World有一个很好的教程,介绍如何使用POI入门,我觉得非常有帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接