sfdatagrid to word table
I have my data generated from RecordsList.
I would like to export this data to word table.
How to do it?
Thank you for contacting Syncfusion Support.
SfDataGrid does not contain direct support to Export data into Word document. Your requirement can be achieved by first export the data into excel after paste into Word document. Please refer the below code snippet,
|
//Add the name space for using the excel and Word
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
private void BtnExportExcel_Click(object sender, EventArgs e)
{
//Save the SfDataGrid data into Excel
var excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, ExcelExportingOptions1());
var workBook = excelEngine.Excel.Workbooks[0];
SaveFileDialog saveFilterDialog = new SaveFileDialog
{
FilterIndex = 2,
Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx|Excel 2013 File(*.xlsx)|*.xlsx",
FileName="Sample"
};
if (saveFilterDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
using (Stream stream = saveFilterDialog.OpenFile())
{
if (saveFilterDialog.FilterIndex == 1)
workBook.Version = ExcelVersion.Excel97to2003;
else if (saveFilterDialog.FilterIndex == 2)
workBook.Version = ExcelVersion.Excel2016;
else
workBook.Version = ExcelVersion.Excel2013;
workBook.SaveAs(stream);
}
}
//Open the Excel File
Excel._Application xlApp = new Excel.Application();
xlApp.Visible = true;
Excel.Workbook workbook = xlApp.Workbooks.Open(saveFilterDialog.FileName);
Excel.Worksheet worksheet = workbook.Sheets[1];
//Create the Document file
Word._Application wdApp = new Word.Application();
wdApp.Visible = true;
Word.Document document = wdApp.Documents.Add();
//paste the Excel used range in Word Document
worksheet.UsedRange.Copy();
document.Range().PasteExcelTable(true, true, true);
workbook.Close();
xlApp.Quit();
} |
UG Link: https://help.syncfusion.com/windowsforms/datagrid/exporttoexcel
Regards,
Vijayarasan S
Thank you. I will try it.
Is there any chance to convert a recordslist into datatable?
I am trying to cast it but it returns null?
cheers,
Mark
Thanks for the update.
Solution 1:
Your requirement can be achieved by directly convert the DataView of first RecordList into DataTable collection. Please refer the below code snippet,
|
//get the record list
var recordList = sfDataGrid.View.Records;
//Solution1:
// get first record dataRowview and convert the DataView directly into Datatable
DataTable dt = (recordList[0].Data as DataRowView).DataView.ToTable();
//get the dt DataTable RowCount
var dtRowsCount = dt.Rows.Count; |
Stack Overflow Link: https://stackoverflow.com/questions/32741104/convert-datarowview-to-datatable-in-c-sharp
https://stackoverflow.com/questions/2122511/simple-way-to-convert-datarow-array-to-datatable
Your requirement can be achieved by add the columns and rows to converting DataTable collection. Please refer the below code snippet,
|
//Solution2:
//get the record list var recordList = sfDataGrid.View.Records;
//create the datatable
DataTable datatable = new DataTable();
int i = 0;
foreach (var record in recordList)
{
//get the DataRow
var datarow = (record.Data as DataRowView).Row;
//first time we need to add the column collection to DataTable
if (i == 0)
{
//add the columns
foreach (DataColumn column in datarow.Table.Columns)
{
datatable.Columns.Add(column.ColumnName, column.DataType);
}
i++;
}
//add the Rows
datatable.Rows.Add(datarow.ItemArray);
}
//get the datatable DataTable RowCount
var datatableRowsCount = datatable.Rows.Count; |
Sample Link: https://www.syncfusion.com/downloads/support/forum/166543/ze/Sample-1354167177
Please let us know if you have any concerns in this.
Regards,
Vijayarasan S
thank you for the response.
I tried it and return null for both
Sample link: https://www.syncfusion.com/downloads/support/forum/166543/ze/Sample676433232
Video Link: https://www.syncfusion.com/downloads/support/forum/166543/ze/RecordListToDataTable-1269021584
Can you please share us below things?
Hi Thank you for the response,
could you please update the collection? instead of using datatable, kindly store it into a collection? Below is only a code sample I took from previous post.
Having this as a datasource, is the procedure in converting into datatable still the same?
List<OrderInfo> list = new List<OrderInfo>();
public class OrderInfo
{
int orderID;
string customerId;
string country;
string customerName;
string shippingCity;
public int OrderID
{
get { return orderID; }
set { orderID = value; }
}
public string CustomerID
{
get { return customerId; }
set { customerId = value; }
}
public string CustomerName
{
get { return customerName; }
set { customerName = value; }
}
public string Country
{
get { return country; }
set { country = value; }
}
public string ShipCity
{
get { return shippingCity; }
set { shippingCity = value; }
}
public OrderInfo(int orderId, string customerName, string country, string customerId, string shipCity)
{
this.OrderID = orderId;
this.CustomerName = customerName;
this.Country = country;
this.CustomerID = customerId;
this.ShipCity = shipCity;
}
}
Thanks for the update.
Your requirement can be achieved by get the record and added into list. After convert the list into DataTable. Please refer the below code snippet,
|
public DataTable ToDataTable<T>(List<T> items)
{
DataTable dataTable = new DataTable("ListIntoDataTable");
//Get all the properties
PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in Props)
{
//Defining type of data column gives proper data table
var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType);
//Setting column names as Property names
dataTable.Columns.Add(prop.Name, type);
}
foreach (T item in items)
{
var values = new object[Props.Length];
for (int i = 0; i < Props.Length; i++)
{
//inserting property values to datatable rows
values[i] = Props[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
//put a breakpoint here and check datatable
return dataTable;
}
private void btnDataTable_Click(object sender, EventArgs e)
{
//get the record list collection
var recordList = sfDataGrid.View.Records;
List<OrderInfo> getList = new List<OrderInfo>();
foreach (var record in recordList)
{
//get the each record by typecast based on underlying data object or underline business object or underline model
//added the record into list
getList.Add(record.Data as OrderInfo);
}
//Convert the List into DataTable
DataTable dataTable= ToDataTable(getList);
//assign the DataTable source into another SfDataGrid
sfDataGrid2.DataSource = dataTable;
} |
Regards,
Vijayarasan S
- 7 Replies
- 2 Participants
-
MJ Mark Jayvee
- Jun 21, 2021 10:12 AM UTC
- Jun 28, 2021 06:18 AM UTC