How to convert an csv sheet to excel(.xlsx) sheet using openxml??

Kishan kumar 1 Reputation point
2021-11-07T11:10:27.757+00:00

Any code available for conversion of csv sheet to excel(.xlsx) sheet using openxml??

Office Open Specifications
Office Open Specifications
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Open Specifications: Technical documents for protocols, computer languages, standards support, and data portability. The goal with Open Specifications is to help developers open new opportunities to interoperate with Windows, SQL, Office, and SharePoint.
138 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Tom Jebo 1,996 Reputation points Microsoft Employee
    2021-11-08T06:31:27.997+00:00

    Hi @Krishankumar6401,

    If you're asking how to create an .xlsx workbook programmatically, then you may be interested in the following resources:

    These are libraries and documentation for creating and manipulating Office Open XML documents using C# (or VB.net) and .Net. There are other libraries and tools out there but these are widely used.

    For specific instructions on how to convert a .csv to .xlsx besides having Excel do it, the Open XML SDK does not contain a function for such conversion and I don't think the PowerTools does either. You would need to find another third party library to help with that or do the work yourself using something like the Open XML SDK.

    The following StackOverflow thread contains some discussion on this:

    https://stackoverflow.com/questions/16732343/converting-excel-file-from-csv-to-xlsx[converting-excel-file-from-csv-to-xlsx][4]

    I hope this helps.

    Best regards,
    Tom Jebo
    Sr Escalation Engineer
    Microsoft Open Specifications Support

    0 comments No comments

  2. Leon Davis 1 Reputation point
    2021-11-15T03:43:01.5+00:00

    You will need to use a third party library to achieve the conversion.

    The following is a code sample shows how to save csv to excel xlsx format based on spire.xls library:

    using Spire.Xls;
    
        namespace CsvtoExcel
        {
            class Program
            {
                static void Main(string[] args)
                {
                 //Load a csv file
                 Workbook workbook = new Workbook();
                 workbook.LoadFromFile(@"D:\Files\Input.csv", ",", 1, 1);
    
                 //Get the first worksheet
                 Worksheet sheet = workbook.Worksheets[0];
                 sheet.Name = "csv to excel";
    
                 //set background color for specific cell ranges
                 sheet.Range["A1:E1"].Style.Color = Color.Chocolate;
                 //set the backgroundcolor=LightBlue of Range["A2:E6"]
                 sheet.Range["A2:E6"].Style.Color = Color.SpringGreen;
                 //set the backgroundcolor=Silver of Range["A7:E12"]
                 sheet.Range["A7:E12"].Style.Color = Color.SkyBlue;
                 //set the backgroundcolor=Silver of Range["A13:E19"]
                 sheet.Range["A13:E19"].Style.Color = Color.Yellow;
    
                 //Save as .xlsx format
                 workbook.SaveToFile("result.xlsx", ExcelVersion.Version2013);        
                }
            }
        }
    

    For more information, you can visit this link:
    https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/CSV-to-Excel-Convert-CSV-to-Excel-with-C-VB.NET.html

    Hope it's helpful.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.