Free Support Forum - aspose.com

How to generate the pdf using dataset values in c# using aspose

Hi All,

I have Dataset it contains data, now i want to download the pdf format.

@knr,
Aspose.Cells can be used for this purpose. As dataset is collection of DataTables, therefore you can easily import data from data table as demonstrated in the following sample code:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET

// Instantiating a Workbook object            
Workbook workbook = new Workbook();

// Obtaining the reference of the worksheet
Worksheet worksheet = workbook.Worksheets[0];

// Instantiating a "Products" DataTable object
DataTable dataTable = new DataTable("Products");

// Adding columns to the DataTable object
dataTable.Columns.Add("Product ID", typeof(Int32));
dataTable.Columns.Add("Product Name", typeof(string));
dataTable.Columns.Add("Units In Stock", typeof(Int32));

// Creating an empty row in the DataTable object
DataRow dr = dataTable.NewRow();

// Adding data to the row
dr[0] = 1;
dr[1] = "Aniseed Syrup";
dr[2] = 15;

// Adding filled row to the DataTable object
dataTable.Rows.Add(dr);

// Creating another empty row in the DataTable object
dr = dataTable.NewRow();

// Adding data to the row
dr[0] = 2;
dr[1] = "Boston Crab Meat";
dr[2] = 123;

// Adding filled row to the DataTable object
dataTable.Rows.Add(dr);

// Importing the contents of DataTable to the worksheet starting from "A1" cell,
// Where true specifies that the column names of the DataTable would be added to
// The worksheet as a header row
worksheet.Cells.ImportData(dataTable, 0,0, new ImportTableOptions() {  });
worksheet.AutoFitColumns();
// Saving the Excel file
workbook.Save("DataImport.out.pdf");

If you are not using Aspose.Cells, please share more details about your requirement along with the sample input data, runnable console application (if any) and expected output file created by some other tool for our reference. We will look into the details and share our feedback accordingly.

Thank you for your suggestion.

One more question: In pdf i need to display two tables using single dataset values is it possible?

@knr,
Yes, it is possible. You can import multiple tables into PDF by calling ImportData() function multiple times as shown in the following modified code:

...
...
// Importing the contents of DataTable to the worksheet starting from "A1" cell,
// Where true specifies that the column names of the DataTable would be added to
// The worksheet as a header row
worksheet.Cells.ImportData(dataTable, 0,0, new ImportTableOptions() {  });
worksheet.Cells.ImportData(dataTable, 5, 0, new ImportTableOptions() { });
worksheet.AutoFitColumns();
// Saving the Excel file
workbook.Save("DataImport.out.pdf");