Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Fri Feb 11, 2011 6:53 am

Hi Experts,

I need load my excel data into my website, but my application only allow me use CSV file. And the data is very large. So, I want to use your Spire.XLS for .NET to help me directly concert my excel workbook into CSV. Could you please tell me how to do it?

thanks

justinx1016
 
Posts: 4
Joined: Fri Dec 24, 2010 1:21 am

Fri Feb 11, 2011 9:37 am

hi justin,

You may write your data one by one with a comma as the separator. And use the SaveToFile method to specify the CSV format to save it. For example: Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
sheet.Range[""A2""].Text = ""hello"";
workbook.SaveToFile(""Sample.csv"","","");"

:P
e-iceblue support
User avatar

iceblue support
 
Posts: 240
Joined: Tue Dec 21, 2010 2:56 am

Mon Jan 23, 2012 10:35 am

Hello,

I tried to convert a workbook, for example DatatableSample.xls, to CSV with V6.0.8.4.
For each conversion, my result is wrong because many special characters parasites the final vue (ý ý ½ È=A ...)

This is what i do :
Code: Select all
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"C:\SpireXls\bin\DatatableSample.xls");
            workbook.SaveToFile("DatatableSample.csv", ExcelVersion.Version97to2003);


Is it a bug ?
Thank you.

docged
 
Posts: 1
Joined: Sat Jan 21, 2012 2:40 am

Tue Jan 24, 2012 5:49 am

Hi justin,

If you want to convert excel worksheet into a csv file, you need a license first.
And then, call SaveToFile/SaveToStream method with a specified separator, for example ,/;/\t etc. Considering internationalization, the result file is saved with Unicode encoding. But different MS-Excel supports different separator and MS-Excel does not support Unicode encoding. So you need do something different.
For Excel 2003:
Code: Select all
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"DatatableSample.xls");
using (MemoryStream buffer = new MemoryStream())
{
      //save to unicode
    workbook.SaveToStream(buffer, ",");
   
    buffer.Position = 0;
   
    //convert to ascii
    using (StreamReader reader = new StreamReader(buffer))
    {
        using (StreamWriter writer = File.CreateText("DatatableSample.csv"))
        {
            String line = null;
            while ((line = reader.ReadLine()) != null)
            {
                writer.WriteLine(line);
            }
        }
    }
}

and for Excel 2010:
Code: Select all
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"DatatableSample.xls");
using (MemoryStream buffer = new MemoryStream())
{
      //save to unicode
    workbook.SaveToStream(buffer, ";");
   
    buffer.Position = 0;
   
    //convert to ascii
    using (StreamReader reader = new StreamReader(buffer))
    {
        using (StreamWriter writer = File.CreateText("DatatableSample.csv"))
        {
            String line = null;
            while ((line = reader.ReadLine()) != null)
            {
                writer.WriteLine(line);
            }
        }
    }
}

and for Excel 2003 & Excel 2010 with multiple languages:
Code: Select all
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"DatatableSample.xls");
workbook.SaveToFile("DatatableSample3.csv", "\t");
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar

harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Mon Dec 22, 2014 8:03 pm

I got round this by specifying the encoding I needed.
.SaveToFile(sOutputPAthFileName, sDelimeter, Encoding.Unicode)
Seems to work fine

erqvu
 
Posts: 4
Joined: Tue Sep 16, 2014 12:06 pm

Tue Dec 23, 2014 3:10 am

Dear erqvu,

Glad to hear that your problem has been resolved.
Please feel free to contact us if you have any problems.
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Return to Spire.XLS