8

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.

5 Answers 5

16

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();
3
  • I'm going to try to follow your example and I'll give you feedback in a while, thanks.
    – Hallaghan
    Jan 24, 2011 at 14:19
  • how can you do the csv converting to array of strings?
    – user65165
    Jul 15, 2015 at 22:02
  • @user65165 If your csv contains only non escaped characters, you can use string.Split otherwise you can use one of the csv parser libraries available
    – vc 74
    Jul 16, 2015 at 7:39
2

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.

2
  • Yes, one of the requirements is that the XLS format is passed.
    – Hallaghan
    Jan 24, 2011 at 13:38
  • In that case, it might be worth looking at excellibrary, which is an older library that writes XLS files. code.google.com/p/excellibrary
    – Quppa
    Jan 24, 2011 at 13:45
0

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.

1
  • Is there any example on the web about your 2nd option?
    – Hallaghan
    Jan 24, 2011 at 12:03
0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.