web-dev-qa-db-ja.com

java)でExcelに書き込む

誰かがJavaでExcelファイルに書き込むための正しい方向に私を向けることができますか?オンラインで見つけたリンクがわかりません。リンクや私がたどることができる何かを送っていただけませんか?

ありがとう、J

8
JJunior

平凡ではありませんが、ApachePOIはそれを行うことができます。ここにいくつかのコード例があります:
http://poi.Apache.org/spreadsheet/examples.html

7
Nikita Rybak

Apache POIのもう1つの代替手段は、 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);
12
matt b

APIの便利な例を投稿します。

JExcelAPI を使用するためのステップバイステップの例:

http://www.vogella.de/articles/JavaExcel/article.html

http://www.Java-tips.org/other-api-tips/jexcel/how-to-create-an-Excel-file.html

[〜#〜] poi [〜#〜] (少し古いものですが便利です)を使用するためのステップバイステップの例:

http://www.javaworld.com/javaworld/jw-03-2004/jw-0322-poi.html

3
YoK

ここでは、サンプルの例、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();    
    }    

}    
2
user2663609
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
satender

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

1
Rahul Raina

JavaからExcelファイルに書き込む必要があるときに ApacheのPOIライブラリ を使用しました。一度コツをつかめば、かなり簡単だと思いました。 Java World has a good tutorial 私が非常に役立つと思ったPOIの使用を開始することについて。

0
FloppyDisk