We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Index zero based error when trying to export datatable to excel

Hello, I'm currently trying to export a datatable to excel using the example https://support.syncfusion.com/kb/article/8133/export-datatable-to-excel-with-formatting-in-c-vb-net

but when I try to execute the funcion it gives me the error

System.FormatException: 'Index (zero based) must be greater than or equal to zero and less than the size of the argument list.'


Here's the code:

// Export class using System.Data;
using Syncfusion.XlsIO;

namespace WinForm.Helpers
{
public class ExcelExport
{
public void Export(DataTable table, string path)
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2016;

IWorkbook workbook = application.Workbooks.Create(1);

IWorksheet worksheet = workbook.Worksheets[0];
worksheet.ImportDataTable(table, true, 1, 1);
worksheet.UsedRange.AutofitColumns();

workbook.SaveAs(path);
}
}
}
}


// Here I use a public variable named Table that is already filled with a datatable when it reaches this step private void excelExportBtn_Click(object sender, EventArgs e)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).ToString();
string fileName = string.Format("{0}.xlsx", NombreTabla);

string fullPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).ToString() + "\\" + fileName;

ExcelExport excelExport = new ExcelExport();
excelExport.Export(Table, fullPath);
MessageBox.Show("Success", "Export");
}


3 Replies 1 reply marked as answer

KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team April 27, 2023 12:45 PM UTC

Hi Alejo,


The shared code snippet looks fine. We suspect there might be an issue with the data in the data table. We request you to share an issue reproducing sample to investigate the query further.


Regards,

Keerthi.



AY Alejo Yanczuk April 27, 2023 01:24 PM UTC

Thanks for the response, I found the problem. 

I was using string.Format() in another line and I was not passing an argument so it returned the index zero based error. The sfdatagrid, excel converter and datatable were fine.


Marked as answer

KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team May 1, 2023 06:05 AM UTC

Alejo, we are glad that the issue is resolved at your end. Please get back to us if you need any further assistance.


Loader.
Up arrow icon