| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 521 人关注过本帖
标题:请高手帮忙解决多个XML文件导入到一个excel的不同工作簿中
只看楼主 加入收藏
zlwin
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-4-18
结帖率:0
收藏
已结贴  问题点数:10 回复次数:1 
请高手帮忙解决多个XML文件导入到一个excel的不同工作簿中
namespace Export_DataGridView_to_Excel
{
    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
            DataSet ds = new DataSet();
            ds.ReadXml("C:\\Products.xml");
         
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            Microsoft.Office.Interop.Excel.Workbook workbook = ExcelApp.Workbooks.Add(Type.Missing);
            ExcelApp.Application.Workbooks.Add(Type.Missing);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
            ExcelApp.Visible = true;

            // get the reference of first sheet. By default its name is Sheet1.
            // store its reference to worksheet

             worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
             worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;

              worksheet.Name = "1";
           
            // changing the name of active sheet
         
            // 更改工作簿的属性
            ExcelApp.Columns.ColumnWidth = 20;

            // Storing header part in Excel
            for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
            {
                ExcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
            }

            // Storing Each row and column value to excel sheet
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                }
            }
            string fileName = String.Empty;
           
              workbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            //ExcelApp.ActiveWorkbook.SaveCopyAs("C:\\test.xls");
            // ExcelApp.ActiveWorkbook.Saved = true;
            ExcelApp.Quit();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
这个事我实现了一个xml文件导入到excel中,请高手帮忙解决多个XML文件导入到一个excel的不同工作簿中,并且对他们命名,我是刚学C#的,请帮忙!
搜索更多相关主题的帖子: excel 工作 private public 
2012-04-18 09:51
心随我意
Rank: 2
等 级:论坛游民
帖 子:14
专家分:13
注 册:2011-6-14
收藏
得分:10 
哎,看不懂呀。。。。。。。。。。
2012-04-19 09:09
快速回复:请高手帮忙解决多个XML文件导入到一个excel的不同工作簿中
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026238 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved