将对象数据写入Excel工作表 [英] write object data to excel sheet

查看:70
本文介绍了将对象数据写入Excel工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在编写一个包含网格视图数据的小应用程序,我可以通过使用一些功能来填充该网格视图,该部分已经完成.但是我需要将该网格视图数据写入excel或csv文件.行并将其作为单个行,其中字段正在写入excel,正在接收一个对象,并且所有字段都分配给了该对象,这也完成了.主要问题是无法将对象数据写入excel或csv文件.
我该如何将对象数据写入excel或csv,请问有谁能帮到我,这对我来说非常紧急.

Hi,
I am writing one small application,that contains grid view data,i can fill that grid view by using some functions,that part is completed.But i need to write that gridview data to excel or csv file.In that grid view am combining some rows and make it as a single row,in which fields am writing in to excel,am taking an object and all the fields are assign to that object,that is also completed.The main problem is am unable to write that object data to excel or csv file.
How can i write that object data to excel or csv,can any one help me please,it is very urgent for me.

推荐答案

用于导出到Excel复制并粘贴下面的事件处理程序进入MainPage.xaml.cs.在这里,我们使用AutomationFactory创建一个Excel实例.然后我们构造一个工作表.

将此代码写在后面的代码中!

For exporting to Excel copy and paste the below event handler into MainPage.xaml.cs. Here we’re using the AutomationFactory to create an instance of Excel. Then we construct a worksheet.

Write this code in the Code Behind!

void ExcelButton_Click(object sender, RoutedEventArgs e)
{
        dynamic excel = AutomationFactory.CreateObject("Excel.Application");
        excel.Visible = true;
 
        dynamic workbook = excel.workbooks;
        workbook.Add();
 
        dynamic sheet = excel.ActiveSheet;
        dynamic cell = null;
        int i = 1;
 
        // Populate the excel sheet
        foreach (var item in (LayoutRoot.DataContext as Bookmarks).Sites)
        {
                cell = sheet.Cells[i, 1];
                cell.Value = item.Title;
 
                cell = sheet.Cells[i, 2];
                cell.Value = item.Uri;
                cell.ColumnWidth = 100;
 
                i++;
        }
}


这篇关于将对象数据写入Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆