Export Scenario

Here is my scenario.

I have an excel template with a set amount of columns and specific headings for each column.

I want to export data from a datatable into this excel template. The problem is the datatable column names and template column names dont match. The template must not be modified in anyway except for data to be added to the existing columns. How would I go about doing this with Aspose.Excel?

Thanks
Mike

Hi Mike,

You can try the Cells.ImportDataTable method while setting isFieldNameShown parameter to false.

Ok the headings stay now but the problem is the columns dont match up correcty. Is there anyway I can assign certain datacolumns to certain columns in the excel template?

Sure you can. Please try Cells.ImportDataColumn method.

Not sure If I am using that properly.

Basically I have a datatable with a row called “OrgName” and I want it exported to a column in the excel doc called “Company_Name” and I dont want the header overwritten because I am using a template.

myCells.ImportDataColumn(dsExcel.Tables[0], false, 0, 0, “OrgName”, false);

Thats what I am currently using but it puts the data in the second column on the excel doc and not the one named Company_Name which is the first column in the excel sheet.

Thanks

In your code, the data in column "OrgName" should be placed in the first column.

Please try the following code to see if it works fine.


DataTable dt = new DataTable("Products");

dt.Columns.Add("Product_ID",typeof(Int32));

dt.Columns.Add("Product_Name",typeof(string));

dt.Columns.Add("Units_In_Stock",typeof(Int32));

dt.Columns.Add("Time", typeof(DateTime));

DataRow dr = dt.NewRow();

dr[0] = 1;

dr[1] = "Aniseed Syrup";

dr[2] = 15;

dr[3] = new DateTime(2004, 1, 15);
dt.Rows.Add(dr);
dr = dt.NewRow();

dr[0] = 2;

dr[1] = "Boston Crab Meat";

dr[2] = 123;

dr[3] = DateTime.Now;

dt.Rows.Add(dr);

Excel excel = new Excel();
excel.Worksheets[0].Cells.ImportDataColumn(dt, false, 0, 0, "Product_Name", false);