1

I'm a french student in engineering school. I have a work to do in my company (yes I work too).

I already developped an application that take xlsx file in input, I use NPOI library. But now I need to take csv in input file, I tried several things without success. I just need to convert a csv file as XSSFworkbook and sheet.

I'm newbie but I really want to improve my competences with c#.

Thanks for your help.

Lec.Gael
  • 31
  • 4
  • 2
    Possible duplicate of [Converting Excel File From .csv To .xlsx](https://stackoverflow.com/questions/16732343/converting-excel-file-from-csv-to-xlsx) – hensing1 Feb 18 '19 at 13:52

1 Answers1

1

I've found on Github a sample that could help you:

class Program
{
        static void Main(string[] args)
    {
        string csvDocument = @"FL_insurance_sample.csv";
        var lines = ReadCsv(csvDocument, delimiter: ',');
        ConvertWithNPOI("NPOI.xlsx", "NPOI", lines);
    }

        private static bool ConvertWithNPOI(string excelFileName, string worksheetName, IEnumerable<string[]> csvLines)
        {
            if (csvLines == null || csvLines.Count() == 0)
            {
                return (false);
            }

            int rowCount = 0;
            int colCount = 0;

            IWorkbook workbook = new XSSFWorkbook();
            ISheet worksheet = workbook.CreateSheet(worksheetName);

            foreach (var line in csvLines)
            {
                IRow row = worksheet.CreateRow(rowCount);

                colCount = 0;
                foreach (var col in line)
                {
                    row.CreateCell(colCount).SetCellValue(TypeConverter.TryConvert(col));
                    colCount++;
                }
                rowCount++;
            }

            using (FileStream fileWriter = File.Create(excelFileName))
            {
                workbook.Write(fileWriter);
                fileWriter.Close();
            }

            worksheet = null;
            workbook = null;

            return true;
    }

    private static bool ConvertWithEPPlus(string csvFileName, string excelFileName, string worksheetName, char delimiter = ';')
    {
            bool firstRowIsHeader = false;

            var format = new ExcelTextFormat();
            format.Delimiter = delimiter;
            format.EOL = "\r";              // DEFAULT IS "\r\n";
            // format.TextQualifier = '"';

            using (ExcelPackage package = new ExcelPackage(new FileInfo(excelFileName)))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(worksheetName);
                worksheet.Cells["A1"].LoadFromText(new FileInfo(csvFileName), format, OfficeOpenXml.Table.TableStyles.Medium27, firstRowIsHeader);
                package.Save();
            }

            return (true);
    }
    private static IEnumerable<string[]> ReadCsv(string fileName, char delimiter = ';')
    {
        var lines = System.IO.File.ReadAllLines(fileName, Encoding.UTF8).Select(a => a.Split(delimiter));
        return (lines);
    }
}

original https://github.com/Leftyx/ConvertCsvToExcel/blob/master/ConvertCsvToExcel/Program.cs

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90