将列添加到Excel 2007工作表 [英] Add column to an Excel 2007 worksheet

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

问题描述

大家好,

我一直在尝试自己解决这个问题.我认为我非常接近完成这项任务.这是我所拥有的

公用的btnFillDG_Click(对象发送者,EventArgs e)
OleDbConnectionStringBuilder();
builder.Provider ="Microsoft.ACE.OLEDB.12.0&";
builder.DataSource = txtSelectExcelFile.Text;
; Excel 12.0; HDR = YES'';

OleDbConnection ExcelDBConnect = new OleDbConnection(builder.ConnectionString); OleDbDataAdapter(从[Sheet1 $]中选择*",ExcelDBConnect);
DataSet dsExcel = new DataSet();表[0];

DataCo lumn myDataColumn;
myDataColumn = new DataColumn();
myDataColumn.DataType = System.Type.GetType(``System.String'');
我的名字=> br/> myDataColumn.ReadOnly = true;
myDataColumn.Unique = true;


dsExcel.Tables [0] .Columns.Add(myData) //需要帮助吗?
dataGridView1.Refresh(); Show(``((2007ExcelFile)Stack Overflowed!'') >
MessageBox.Show(抛出一个OleDb错误!" +"n" + ex_oledb2.Message);
/>关于我如何添加列的任何建议将不胜感激.

谢谢,

RA

Hello Everyone,

I have been trying to solve this problem on my own for some time.  I think I am very close to accomplishing this task.  Here is what I have

   public void btnFillDG_Click(object sender, EventArgs e)
        {
            try
            {
               
                OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder();
                builder.Provider = "Microsoft.ACE.OLEDB.12.0";
                builder.DataSource = txtSelectExcelFile.Text;
                builder["Extended Properties"] = "Excel 12.0;HDR=YES";

                OleDbConnection ExcelDBConnect = new OleDbConnection(builder.ConnectionString);
               
                ExcelDBConnect.Open();
                OleDbDataAdapter adpExcel = new OleDbDataAdapter("select * from [Sheet1$]", ExcelDBConnect);
                DataSet dsExcel = new DataSet();
                adpExcel.Fill(dsExcel);
                dataGridView1.DataSource = dsExcel.Tables[0];

                DataColumn myDataColumn;
                myDataColumn = new DataColumn();
                myDataColumn.DataType = System.Type.GetType("System.String");
                myDataColumn.ColumnName = "Test";
                myDataColumn.ReadOnly = true;
                myDataColumn.Unique = true;

                dsExcel.Tables[0].Columns.Add(myDataColumn);  //I NEED HELP HERE?
                dataGridView1.Refresh();
             
            }

            catch (StackOverflowException stack_ex2)
            {
                MessageBox.Show("(2007ExcelFile)Stack Overflowed!" + "\n" + stack_ex2.Message);
            }

            catch (OleDbException ex_oledb2)
            {
                MessageBox.Show("An OleDb Error Thrown!" + "\n" + ex_oledb2.Message);
            }
           
              
        }

Any advice as to how I add a column would be greatly appreciated.

Thank You,

RA

推荐答案

正如我在博客中所讨论的,部落知识:使用Office Interops 一样,这是一种神圣任何Office文档的内部工作原理以及如何通过互操作性是记录所需过程的宏.检查完vba代码后,它将显示设置更改和其他有趣的项,这些项可以引导有关互操作性的部落知识.大多数对象调用在幕后都是相同的...祝您好运.

否则,此问题在C#组中不会采取很多措施...尝试在Office开发中的讨论,或寻求更好的答复或有关自动化问题的讨论论坛,有关互操作性问题.
As I discuss on my blog entitled, Tribal Knowledge: Working with Office Interops , one way to divine the inner workings of any office document and how to manage it via the interops is to record a macro of the process needed. Once done examine the vba code, it will show settings changes and other items of interest that can lead the way through the tribal knowledge of the interops. Most the object calls are the same under the covers...good luck.

Otherwise this question won't get much action in the C# group...try posting it in the Discussions in Office Development or for a better responses or Discussions in Automation forum for interop questions.


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

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