将 CSV 转换为 XLS

发布于 2024-10-13 12:25:59 字数 140 浏览 5 评论 0原文

我正在一个以块分隔的 Web 应用程序中工作,我从我的同事那里获取了一个 CSV 对象,我必须将其转换为 XLS 才能传递到他们构建的 Excel 处理器中。 该 CSV 对象由字符“;”分隔。

我想知道如何以编程方式将 CSV 对象转换为 XLS。

I'm working in a web application separated in blocks and I'm getting a CSV object from a work mate of mine which I must convert into XLS to be passed into an Excel Processor they built.
This CSV object is delimited by the character ";".

What I'd like to know is how I can convert the CSV object into XLS programatically.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

热情消退 2024-10-20 12:25:59

您应该很容易将 CSV 对象转换为字符串数组的数组,然后按照以下示例执行操作(您需要添加对 Microsoft.Office.Interop.Excel 的引用):

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application excel = new Excel.Application();
Excel.Workbook workBook = excel.Workbooks.Add();
Excel.Worksheet sheet = workBook.ActiveSheet;

var CsvContent = new string[][]
{
    new string[] {"FirstName", "UserName", "PostCode", "City"},
    new string[] {"John", "Smith", "4568", "London"},
    new string[] {"Brian", "May", "9999", "Acapulco"}
};

for (int i = 0; i < CsvContent.Length; i++)
{
    string[] CsvLine = CsvContent[i];
    for (int j = 0; j < CsvLine.Length; j++)
    {
        sheet.Cells[i + 1, j + 1] = CsvLine[j];
    }
}

workBook.SaveAs(@"C:\Temp\fromCsv.xls");
workBook.Close();

It should be easy for you to convert the CSV object into an array of arrays of strings and then do like in the following example (you'll need to add a reference to Microsoft.Office.Interop.Excel):

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application excel = new Excel.Application();
Excel.Workbook workBook = excel.Workbooks.Add();
Excel.Worksheet sheet = workBook.ActiveSheet;

var CsvContent = new string[][]
{
    new string[] {"FirstName", "UserName", "PostCode", "City"},
    new string[] {"John", "Smith", "4568", "London"},
    new string[] {"Brian", "May", "9999", "Acapulco"}
};

for (int i = 0; i < CsvContent.Length; i++)
{
    string[] CsvLine = CsvContent[i];
    for (int j = 0; j < CsvLine.Length; j++)
    {
        sheet.Cells[i + 1, j + 1] = CsvLine[j];
    }
}

workBook.SaveAs(@"C:\Temp\fromCsv.xls");
workBook.Close();
风情万种。 2024-10-20 12:25:59

输出是否需要采用旧版 XLS 格式?如果 XLSX 可接受,EPPlus 是一个用于编写电子表格的出色 .NET 库。旧版 excellibrary 可以生成 XLS 文件。

解析 CSV 文件(只需小心双引号)和编写输出电子表格只需几行代码。

Does the output need to be in the legacy XLS format? If XLSX is acceptable, EPPlus is a great .NET library for writing spreadsheets. The older excellibrary can produce XLS files.

Only a few lines of code should be necessary for parsing the CSV file (just be careful of double quotation marks) and writing the output spreadsheet.

流星番茄 2024-10-20 12:25:59

一种选择是使用第 3 方库创建 XLS 文件,另一种选择是使用 COM 互操作操作 Excel 打开 CSV 文件并将其另存为 XLS。

One option is to use a 3rd party library to create the XLS file, and the other is to use COM interop to manipulate Excel into opening the CSV file and save it as XLS.

欢你一世 2024-10-20 12:25:59

C# Excel 将数据从 CSV 导入 Excel 有帮助吗?它似乎解决了你需要做的事情。

Would this C# Excel import data from CSV into Excel help? It seems to address what you need to do.

遥远的绿洲 2024-10-20 12:25:59
    using Excel = Microsoft.Office.Interop.Excel;

    public void Convert_CSV_To_Excel()
    {

        // Rename .csv To .xls
        System.IO.File.Move(@"d:\Test.csv", @"d:\Test.csv.xls");

        var _app = new Excel.Application();
        var _workbooks = _app.Workbooks;

        _workbooks.OpenText("Test.csv.xls",
                                 DataType: Excel.XlTextParsingType.xlDelimited,
                                 TextQualifier: Excel.XlTextQualifier.xlTextQualifierNone,
                                 ConsecutiveDelimiter: true,
                                 Semicolon: true);
        // Convert To Excle 97 / 2003
        _workbooks[1].SaveAs("NewTest.xls", Excel.XlFileFormat.xlExcel5);

        _workbooks.Close();
    }
    using Excel = Microsoft.Office.Interop.Excel;

    public void Convert_CSV_To_Excel()
    {

        // Rename .csv To .xls
        System.IO.File.Move(@"d:\Test.csv", @"d:\Test.csv.xls");

        var _app = new Excel.Application();
        var _workbooks = _app.Workbooks;

        _workbooks.OpenText("Test.csv.xls",
                                 DataType: Excel.XlTextParsingType.xlDelimited,
                                 TextQualifier: Excel.XlTextQualifier.xlTextQualifierNone,
                                 ConsecutiveDelimiter: true,
                                 Semicolon: true);
        // Convert To Excle 97 / 2003
        _workbooks[1].SaveAs("NewTest.xls", Excel.XlFileFormat.xlExcel5);

        _workbooks.Close();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文