如何使用Java创建基于CSV文件的Excel文件?

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

How to create an Excel file based on CSV file using Java?

javaexcelcsv

提问by Sameek Mishra

I have a requirement to create an XLS file on the basis of a CSV file using Java.

我需要使用 Java 在 CSV 文件的基础上创建 XLS 文件。

Please suppest to me which API is best to create excel file.

请告诉我哪个 API 最适合创建 excel 文件。

Thanks

谢谢

采纳答案by aioobe

I suggest you use the Apache POI framework(specifically the HSSF / XSSF API) for writing out the XLS file.

我建议您使用Apache POI 框架(特别是HSSF / XSSF API)来写出 XLS 文件。

For reading a CSV file I suggest you use OpenCSVas it will take care of escaped characters etc for you.

对于读取 CSV 文件,我建议您使用OpenCSV,因为它会为您处理转义字符等。

Putting together the POI example from hereand the OpenCSV example from heregives you this:

从放在一起的POI例如这里从OpenCSV例子在这里给大家:

import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import au.com.bytecode.opencsv.CSVReader;

class Test {
    public static void main(String[] args) throws IOException {
        Workbook wb = new HSSFWorkbook();
        CreationHelper helper = wb.getCreationHelper();
        Sheet sheet = wb.createSheet("new sheet");

        CSVReader reader = new CSVReader(new FileReader("data.csv"));
        String[] line;
        int r = 0;
        while ((line = reader.readNext()) != null) {
            Row row = sheet.createRow((short) r++);

            for (int i = 0; i < line.length; i++)
                row.createCell(i)
                   .setCellValue(helper.createRichTextString(line[i]));
        }

        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream("workbook.xls");
        wb.write(fileOut);
        fileOut.close();
    }
}

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by Vivek Goel

There are many check

有很多支票

http://jexcelapi.sourceforge.net/

http://jexcelapi.sourceforge.net/

you will get a lot of alternative. Just google.

你会得到很多选择。只是谷歌。

回答by Andreas Dolk

Apache POIis a library that can handle all kinds of microsoft office documents, including MS Excel. You can read the content of the csv file with simple java code and use that library to create and save a MS Excel document.

Apache POI是一个可以处理各种微软办公文档的库,包括 MS Excel。您可以使用简单的 java 代码读取 csv 文件的内容,并使用该库创建和保存 MS Excel 文档。

回答by Shahzad Latif

You may try Aspose.Cells for Java. You can use this component to open a CSV fileand save it as XLS fileusing Java. It also helps you work with different Excel file versions.

你可以试试Aspose.Cells for Java。您可以使用此组件打开 CSV 文件并使用 Java将其保存为 XLS 文件。它还可以帮助您处理不同的 Excel 文件版本。

Disclosure: I work as developer evangelist at Aspose.

披露:我在 Aspose 担任开发人员布道者。

回答by djangofan

Using Groovy, I would do it this way:

使用 Groovy,我会这样做:

// groovy to generate large csv file
def GROOVY_HOME = new File( System.getenv('GROOVY_HOME') )
if ( !GROOVY_HOME.canRead() ) {
  println( "Missing environment variable GROOVY_HOME: '${GROOVY_HOME}'" )
  System.exit(0)
}

File file = new File("csv.csv")
if ( file.exists() ) {
    assert file.delete()
    assert file.createNewFile()
}

boolean append = true
FileWriter fileWriter = new FileWriter(file, append)
BufferedWriter buffWriter = new BufferedWriter(fileWriter)

buffWriter.write "sdiType=ReferenceValue,,,\n"
buffWriter.write "ListName,ListStartDate,Value,ValueStartDate\n"
println( "Writing to file 'csv.csv'" )

def y = 5000
while ( y-- > 0 ) {
    buffWriter.write "test" + y + ",1/1/2001,2008,1/1/2001\n"
}

buffWriter.flush()
buffWriter.close()