We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Cannot save as .csv

When I try to save a spreadsheet as .csv the result is blank. If I save as .xls it works as expected.

var workbook = new Workbook();
var i = workbook.Worksheets.Add();
var worksheet = workbook.Worksheets[i];
var cells = worksheet.Cells;

cells[“A1”].PutValue(“testing”);
workbook.Save(“h:\MarcieTest.csv”, SaveFormat.CSV);
// this works workbook.Save(“h:\MarcieTest.xls”);
What am I doing wrong?

Hi,


The reason is simple, the CSV format is based on the active sheet (for the conversion). For your code, by default, first worksheet would be active which is actually blank. Actually you inserting value into the second worksheet’s cell A1. You may change your code a bit to activate your second worksheet, or you may hide the first worksheet.

See the sample code that works absolutely fine.

Sample code:
Aspose.Cells.License license = new Aspose.Cells.License();
license.SetLicense(@“E:\Licenses\Aspose.Cells.lic”);
var workbook = new Workbook();
var i = workbook.Worksheets.Add();
var worksheet = workbook.Worksheets[i];
var cells = worksheet.Cells;
cells[“A1”].PutValue(“testing”);
workbook.Worksheets.ActiveSheetIndex = i;
workbook.Save(“e:\test2\MarcieTest.csv”, SaveFormat.CSV);

Hi,

I’m currently experiencing the same issue, with version 9.0, and the active sheet index is correctly set… here’s my code:

    public static void Export(this IDataReader @this, Stream stream, string format)
    {
        using (var book = new Workbook())
        {
            book.Worksheets.Clear();
            var sheet = book.Worksheets.Add("References");

            // Data Table import to the worksheet, inserting from A1 cell of sheet
            sheet.Cells.ImportDataReader(@this, true, 0, 0, true,
                CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern, false);

            // Apply Hearder Row/First Row text to Bold
            Style objStyle = book.CreateBuiltinStyle(BuiltinStyleType.Header1);
            StyleFlag objStyleFlag = new StyleFlag { FontBold = true, WrapText = true };

            sheet.Cells.ApplyRowStyle(0, objStyle, objStyleFlag);

            // Fit columns width to contents
            sheet.AutoFitColumns(new AutoFitterOptions { MaxRowHeight = 30 });

            book.Save(stream, BuildSaveOptions(format));
        }
    }

…works fine with Excel file streams, but not CSV!

Also, using to code to export into a MemoryStream results in incomplete documents for every format!

@miguelhasse,

The issue might be due to your own code as I do not see any line of code where you set the active sheet index. Please see my previous reply in the thread. If you still could not evaluate your issue, kindly create a simple console demo application (runnable), you may create DataSet and DataTable dynamically in the code, zip it and post us here to reproduce the issues (e.g blank sheet and “incomplete documents”) on our end, we will check it soon.

PS. It is better that you could use our latest version’fix: Aspose.Cells for .NET v17.8.

Thank you.