I have my data generated from RecordsList.
I would like to export this data to word table.
How to do it?
|
//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();
} |
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
|
//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; |
|
//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; |
thank you for the response.
I tried it and return null for both
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;
}
}
|
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;
} |