标题:请高手帮忙解决多个XML文件导入到一个excel的不同工作簿中
取消只看楼主
zlwin
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-4-18
结帖率:0
已结贴  问题点数:10 回复次数:0 
请高手帮忙解决多个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



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-366066-1-1.html




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

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