Excel import from data table

Hi,


I am trying to import data from data table. It will but in Sheet2 instead of Sheet1. I have used sample code from your site.

Herewith i have attached sheet for your reference.

Also is it possible to import from dataset? If its so please send sample code.

Please clarify.

Hi Rajkumar,


Thank you for contacting Aspose support.

You must be referring to the second worksheet while retrieving the instance of worksheet from the collection. Please note, the WorksheetCollection has 0 based index so when you call the following statement, it will get the second worksheet from the collection.

C#

var sheet = book.Worksheets[1];

Replace the 1 with 0, and it should retrieve the first worksheet for further processing.

C#
//Initialize Workbook var book = new Workbook(); //Get instance of first worksheet var sheet = book.Worksheets[0]; //Get instance of first worksheet's cells var cells = sheet.Cells; //Import DataTable to first worksheet cells
//Choose the best overload that suits the requirement cells.ImportDataTable(dataTable, true, "A1");

Regarding the other inquiry, a DataSet is the collection of DataTables so you can easily iterate over the DataTable collection in a DataSet to import the data to worksheet cells as elaborated above.

Please feel free to write back in case you need our further assistance with Aspose APIs.

Hi,


Thanks for quick response and it worked fine.

I have one more requirement.

I want a borders for the specific range with inner borders like a table.

Please send me the sample code with C#.

Attached sample for your reference

Regards,
Rajkumar J

Hi Rajkumar,


Thank you for the confirmation on the previously reported problem.

Please check the following code snippet that first creates a range of cells from A1 to D5 then applies the border style for the range cells and range as a whole.

C#

var book = new Workbook();
var sheet = book.Worksheets[0];
var range = sheet.Cells.CreateRange(“A1”, “D5”);
var style = book.Styles[book.Styles.Add()];
style.Borders.SetStyle(CellBorderType.Thin);
range.ApplyStyle(style, new StyleFlag() {BottomBorder = true, LeftBorder = true, RightBorder = true, TopBorder = true });
range.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Medium, System.Drawing.Color.Black);
range.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Medium, System.Drawing.Color.Black);
range.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Medium, System.Drawing.Color.Black);
range.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Medium, System.Drawing.Color.Black);
book.Save(“D:/temp/output.xlsx”, SaveFormat.Xlsx);


Please also check the detailed article on Adding Cell Borders, and feel free to write back in case you need our further assistance.