C#中如保获得Excel单元格的名称(此名称是把单元格名称修改之后的名称,如:把A1改为liu,liu就代表A1),现在我要在代码中,取到这个liu.各位大侠怎么实现,请给点代码,谢谢!!

解决方案 »

  1.   

    private void ListRecentFiles()
    {
        Excel.Range rng = (Excel.Range)Application.
            get_Range("RecentFiles", missing).Cells[1, 1];
        for (int i = 1; i <= Application.RecentFiles.Count; i++)
        {
            rng.get_Offset(i - 1, 0).Formula = 
                Application.RecentFiles[i].Name;
        } 
    }
      

  2.   

    http://www.microsoft.com/china/msdn/library/office/office/VSTOCSharpDev.mspx?mfr=true
      

  3.   

    private void btnAdd_Click(object sender, EventArgs e)
    {
    lblID.Text=list.SelectedValue;
    if(lblID.Text.Trim()=="")
    {
    MessageBox.Show(Page,"请先选择大纲。");
    return;
    } if(listCoefficient.SelectedValue.Trim()=="")
    {
    MessageBox.Show(Page,"请先选择难度系数。");
    return;
    } if(fileUp.PostedFile.FileName=="")
    {
    MessageBox.Show(Page,"请先选择文件。");
    return;
    }
    string filePath="",fileExtName="",mFileName="",mPath="";
    StringBuilder strMsg = new StringBuilder("上传的文件信息:<hr color=red>");
    if("" != fileUp.PostedFile.FileName)
    {
    filePath =fileUp.PostedFile.FileName;//取得文件路径
    fileExtName= filePath.Substring(filePath.LastIndexOf(".")+1);
    if(fileExtName.ToUpper()!="XLS")
    {
    MessageBox.Show(Page,"文件必须为EXCEL表格。");
    return;
    } try
    {
    //取得与 Web 服务器上的指定虚拟路径相对应的物理文件路径。
    mPath=Server.MapPath("upfile/");
    mFileName=filePath.Substring(filePath.LastIndexOf("\\")+1);;//取得文件名
    strMsg.Append("上传的文件类型:" + fileUp.PostedFile.ContentType.ToString() + "<br>");
    strMsg.Append("客户端文件地址:" + fileUp.PostedFile.FileName + "<br>");
    strMsg.Append("上传文件的文件名:" + mFileName + "<br>");
    strMsg.Append("上传文件的扩展名:" + fileExtName);
    //保存上传文件到指定的目录
    fileUp.PostedFile.SaveAs(mPath + mFileName);
    }
    catch(Exception error)
    {
    Response.Write(error.ToString());
    }
    Add(mPath,mFileName,int.Parse(lblID.Text),int.Parse(listCoefficient.SelectedValue.Trim()));
    }
    }//mPath,mFileName为路径和文件名。
    private void Add(string mPath,string mFileName,int OutLineID,int CoefficientID)
    {
    DataTable myTable=new DataTable("ExcelTable");
    String strConnection="Provider=Microsoft.Jet.OleDb.4.0;data source="+mPath+mFileName+";Extended Properties=Excel 8.0;";
    OleDbConnection objConnection=new OleDbConnection(strConnection); String strSQL="SELECT * FROM [Sheet1$]";
    OleDbCommand objCommand=new OleDbCommand(strSQL,objConnection);
    OleDbDataAdapter objDataAdapter=new OleDbDataAdapter(objCommand);
    objConnection.Open();
    objDataAdapter.Fill(myTable);
    objConnection.Close();

    if(myTable.Rows.Count<1)
    {
    MessageBox.Show(Page,"表格内容为空。");
    return;
    }
    StringBuilder strInSertSQL=new StringBuilder();
    foreach(DataRow myRow in myTable.Rows)
    {
    strInSertSQL.Append("insert into Judge(");
    strInSertSQL.Append("CoefficientID,OutlineID,Title,isRight");
    strInSertSQL.Append(")");
    strInSertSQL.Append(" values (");
    strInSertSQL.Append(""+CoefficientID+",");
    strInSertSQL.Append(""+OutLineID+",");
    strInSertSQL.Append("'"+myRow["题"].ToString()+"',");
    strInSertSQL.Append("'"+myRow["答案"].ToString()+"'");
    strInSertSQL.Append(");");
    }
    DbHelperSQL.ExecuteSql(strInSertSQL.ToString());
    MessageBox.Show(Page,"导入成功。");
    return;
    }
      

  4.   

    谢谢daishengs(横舟摆渡) 
    但是直接用Open打开
     this.excelApp.Workbooks.Open(fileName, vk_update_links, vk_read_only, vk_format, vk_password,vk_write_res_password, vk_ignore_read_only_reco,vk_origin,vk_delimiter, vk_editable, vk_notify, vk_converter, vk_add_to_mru,vk_local, vk_corrupt_load);按照上面思路,应该怎么实现,请大家帮帮忙
      

  5.   

    怎么这么多人问这方面的问题啊!
    看看我写的,希望能给你帮助!
    http://www.cnblogs.com/linfuguo/archive/2006/08/19/480976.htmlGood Luck!
      

  6.   

    参见http://www.microsoft.com/china/msdn/library/office/office/VSTOCSharpDev.mspx?mfr=true
      

  7.   

    联系我:[email protected]
    我给你发个巨强的VBA资料
      

  8.   

    近來是不是用c#做office開發的很吃香,好多人在做喔...
      

  9.   

    Function getName(addr As String) As String    
        
        Set nms = ActiveWorkbook.Names
        Set wks = Worksheets(1)
        For n = 1 To nms.Count
            If addr = nms(n).RefersToRange.Address Then
                getName = nms(n).Name
                MsgBox nms(n).Name
                Exit Function
            End If
        Next
        
        MsgBox "No name!"
        
    End Function
    Sub test()
        
        Call getName("$A$1")End Sub
      

  10.   

    把问题重贴一下C#中如保获得Excel单元格的名称(此名称是把单元格名称修改之后的名称,如:把A1改为liu,liu就代表A1),现在我要在代码中,取到这个liu.各位大侠怎么实现,请给点代码,谢谢!!
      

  11.   

    用nexcel.dll 网上当一个
      

  12.   

    问题,还是没解决。今日仍在线,请问怎么用nexcel.dll 实现我的问题。
    请给点代码,谢谢
      

  13.   

    楼主要干什么?
    是只要读出那个liu还是要引用liu所在的那个格子?
    给你一点参考
    http://msdn2.microsoft.com/zh-CN/library/microsoft.office.tools.excel.workbook.names.aspx
      

  14.   

    顶,LZ应该认真研究一下Excel的对象模型。求人不如求己
      

  15.   


    to livode(啊水) 
    我是要读出liu,因为我已把A1单元格的名称,改成了liu
      

  16.   

    参考http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/ExcelObj.asp中的The Name Class and Names Collection部分
      

  17.   

    to   enutter(The Notorious G.W.W.) 
    你好,我看了你说的The Name Class and Names Collection部分
    但这个只有获取这个改了名字单元格的值。
    没有看到怎么获取,单元格名字的代码
      

  18.   

    ActiveCell.Name.Name 就是单元格的名字
      

  19.   

    to  enutter(The Notorious G.W.W.) 
    你好,
    ActiveCell.Name.Name 最后一个Name点不出来
      

  20.   

    to lz:
    http://msdn2.microsoft.com/zh-CN/library/microsoft.office.tools.excel.workbook.names.aspx里有很详细的说明了。
    所有的别名都在workbook.names里,用索引取出来。
      

  21.   

    //oWS是操作的Excel._Worksheet 
    oWS.get_Range("E2:E6", Missing.Value ).Name="liu"
      

  22.   

    to 
    livode(啊水)
    你好,
    我的开发平台,是vs2003
    你所给的资料,是vs2005的,有的包,在vs2003里没有
      

  23.   

    那位同仁要是知道怎么获得单元格名字的.请留个联系方式,QQ,或msn
    当面请教.谢谢
      

  24.   

    环境:vs2003,office 2000
    功能:给excel的第一行第二列起个别名"xxxx",并将此名显示在第1列中。
    代码:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using Excel;namespace 读取excel单元格名称
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.OpenFileDialog openFileDialog1;
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(8, 8);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "请选择文件";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    System.Windows.Forms.Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    this.openFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
    this.openFileDialog1.ShowDialog();

    if(this.openFileDialog1.FileName.Equals(""))
    {
    MessageBox.Show("请选择一个Excel文件!");
    }
    else
    {
    Excel.Application excel = new Excel.ApplicationClass();
    Excel.Workbook workbook = excel.Workbooks.Open(this.openFileDialog1.FileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);

    excel.ActiveWorkbook.Names.Add("xxxx","=Sheet1!R1C2",true,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing); for(int i=1;i<=excel.ActiveWorkbook.Names.Count;i++)
    {
    Excel.Range range = excel.get_Range("A"+i.ToString(),Type.Missing);
    MessageBox.Show(excel.ActiveWorkbook.Names.Item(i,Type.Missing,Type.Missing).ToString());
    range.Select();
    range.Value2 = excel.ActiveWorkbook.Names.Item(i,Type.Missing,Type.Missing).Name;
    }
    workbook.Save();
    workbook.Close(false,Type.Missing,Type.Missing);
    }
    }
    }
    }