Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
C# - I want to convert a csv file to xlsx format on a server where office is not installed.

What I have tried:

//Using Interop
                //Application app = new Application();
                //Workbook wb = app.Workbooks.Open(Util.CSVFileName);
                //wb.SaveAs(Util.XLSXFileName, XlFileFormat.xlOpenXMLWorkbook, AccessMode: XlSaveAsAccessMode.xlExclusive);
                //wb.Close();
                //app.Quit();
Posted
Updated 10-Jul-19 2:57am
Comments
Richard MacCutchan 26-Sep-16 8:45am    
And what happens when you do?
Member 12613458 26-Sep-16 9:52am    
I need a saveas non office version. something like Open Excel could help
Member 12613458 26-Sep-16 8:48am    
I need a saveas non office version. something like Open XML could help
Karthik_Mahalingam 26-Sep-16 9:50am    
Always use  Reply   button to post comments/query to the concerned user, so that the user gets notified and respond to your text.
Patrice T 26-Sep-16 10:37am    
Do you know that Excel can open a .csv file directly ?

 
Share this answer
 
v2
Comments
Karthik_Mahalingam 27-Sep-16 11:30am    
Counter 5! :)
Maciej Los 27-Sep-16 11:32am    
Thank you Karthik.
 
Share this answer
 
Comments
Maciej Los 27-Sep-16 11:28am    
5ed!
Karthik_Mahalingam 27-Sep-16 11:30am    
ThAnK YoU Maciej :)
C#
if (File.Exists(Util.XLSXFileName))
                {
                    File.Delete(Util.XLSXFileName);
                }
                //Save as xlsx using Spire
                Workbook workbook = new Workbook();
                workbook.LoadFromFile(Util.CSVFileName, ",", 1, 1);
                Worksheet sheet = workbook.Worksheets[0];
                sheet.Name = "csv to excel";
                workbook.SaveToFile(Util.XLSXFileName, ExcelVersion.Version2010);

                //Delete Evaluation Warning using EPPlus
                var workbookFileInfo = new FileInfo(Util.XLSXFileName);
                using (var excelPackage = new ExcelPackage(workbookFileInfo))
                {
                    var worksheet = excelPackage.Workbook.Worksheets.SingleOrDefault(x => x.Name == "Evaluation Warning");
                    excelPackage.Workbook.Worksheets.Delete(worksheet);
                    excelPackage.Save();
                }
 
Share this answer
 
Comments
Maciej Los 27-Sep-16 11:31am    
Glad to see that you've found a solution. Seems, you did follow the steps provided in my answer. A rule of CP QA borad is: mark all valuable answers as solution. Can i count on you?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900