可以在office中录制宏实现批量操作。

解决方案 »

  1.   

    将excel数据读取到datagridviewjfd ,编辑修改后保存为 wordpublic void ExportDataGridView(DataGridView srcDgv)
    {
           SaveFileDialog sfile = new SaveFileDialog();
           sfile.AddExtension = true;
           sfile.DefaultExt = ".doc";
           sfile.Filter = "(*.doc)|*.doc";
           if (sfile.ShowDialog() == DialogResult.OK)
           {
                   object path = sfile.FileName;
                   Object none = System.Reflection.Missing.Value;
                   Word.Application wordApp = new Word.Application();
                   Word.Document document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
                    //建立表格
                   Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count , srcDgv.Columns.Count, ref none, ref none);
                   try
                   {
                       for (int i = 0; i < srcDgv.Columns.Count; i++)//设置标题
                       {
                            table.Cell(0, i + 1).Range.Text = srcDgv.Columns[i].HeaderText;
                       }
                       for (int i = 1; i < srcDgv.Rows.Count; i++)//填充数据
                       {
                           for (int j = 0; j < srcDgv.Columns.Count; j++)
                           {
                                table.Cell(i+1, j + 1).Range.Text = srcDgv[j, i - 1].Value.ToString();
                           }
                       }
                       document.SaveAs(ref path, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);
                       document.Close(ref none, ref none, ref none);
                       MessageBox.Show("导出成功!");
                   }
                   finally
                   {
                        wordApp.Quit(ref none, ref none, ref none);
                   }
           }
    }
      

  2.   


        public  void LoadDataFromExcel(string filePath, string name)
            {
                try
                {
                                  string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + filePath + ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";//导入时包含Excel中的第一行数据,并且将数字和字符混合的单元格视为文本进行导入
                     OleDbConnection OleConn = new OleDbConnection(strConn);
                    OleConn.Open();
                    string sql = "SELECT * FROM [" + name + "$]";//可是更改Sheet名称,比如sheet2,等等  
                    OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
                    DataSet OleDsExcle = new DataSet();
                    OleDaExcel.Fill(OleDsExcle, name);
                    dataGridView1.DataSource = OleDsExcle.Tables[0].DefaultView;  
                    OleConn.Close();
                             }
                catch (Exception err)
                {
                    MessageBox.Show("数据绑定Excel失败! 失败原因:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                              }
            }
    //读取EXCEL到 datagridview1
     LoadDataFromExcel(Application.StartupPath +"\\test.xls", "Sheet1");
    // 将修改后datagridview1里的数据保存到word  ExportDataGridView(dataGridView1);
      

  3.   

    你可以自己搜索一下,如何用c#将excel工作表内容拷贝到剪贴板,以及如何从剪贴板上把内容粘贴到word文档。两方面的代码,加起来也不过10行而已。
      

  4.   


    很感谢你的代码,但是你这个代码我看了一下,貌似只是格式转换,写出了数据流的读写,但是我想知道excel种的表格数据怎么在C#中进行编辑(加边框,颜色填充,居中,排序等等,有没有API?)麻烦您指教,谢谢。
      

  5.   


    很感谢你的代码,但是你这个代码我看了一下,貌似只是格式转换,写出了数据流的读写,但是我想知道excel种的表格数据怎么在C#中进行编辑(加边框,颜色填充,居中,排序等等,有没有API?)麻烦您指教,谢谢。
    (加边框,颜色填充,居中,排序等等,要实现这些功能,可能将EXCEL嵌入到WInFORM,不知道NPOI有没有这个功能,还没有写过
      

  6.   

    能不能说的具体点啊。用vb的什么东东啊?是VBA不是VB
    你百度一下吧
      

  7.   

    能不能说的具体点啊。用vb的什么东东啊?是VBA不是VB
    你百度一下吧
    看了一下  貌似有门  不过以前没有用过  我想进一步问下  解决我碰到的问题需要哪些关键措施:开发环境,以及应当关注VBA的哪些要点才能解决我面对的问题。。谢谢