用C#读取Excel文件时,怎样从指定单元格开始向右向下读取数据?例如:SQL语句为:SELECT * FORM [Sheet1$] 是从工作薄1中有数据的第一个单元格开始读取数据。
那么要从指定单元格开始读取应该怎么办?

解决方案 »

  1.   

    你就rs.NextResult 多少个单元格.
    或是read多少次.
    :)
      

  2.   

    先读到C#的datatable里,再整理数据吧
      

  3.   

    你这样读的话.默认都是从第二行开始读取的.ps:因为Sql会默认都第一行当成是列名,明白了吗?如果要读的话.你可以开始读取你Select出来的DataTable的列名.再读值.或在Excel前自已插入一行
      

  4.   

    对,是的。默认第一行是列名。如果Excel工作表中第一行第一列有数据,但是我想从第三行第三列开始读(此行在DataTable中是列名),应该怎么办?
      

  5.   

    可以用office的控件操作excel
    那个能指定重哪一格读取数据
      

  6.   

    全都读出来又无所谓, 关键是用的时候再做选择.
    所以你只要读到DataTable中去, 再选择数据就行啦
      

  7.   

    全都读出来,在DataTable中处理也可以,这样必须要保证在Excel工作表中有数据的第一行必须是数据库列名。如果我想将工作表中有数据的第二行当作数据库列名(即:从有数据的第二行单元格开始读),那么有没有好的解决方法?能不能用在SQL语句中定位单元格呢?
      

  8.   

    各位大哥:    小弟已将问题解决了,拿出来共享如下:    在工作表中,用鼠标左键拖拽,选取要读取数据的区域,将其定义成为表,从菜单中选择'插入 '->'名称'->'定义',在'定义名称'对话框出现后,键入表的名字:TestTable。
        在C#中定义SQL语句:SELECT * FROM TestTable,读取到DataTable中的数据就是所想得到的数据了。
      

  9.   

    给你一个我自已写的类 只要有一个dataset 就可以了 dataset 里面有多少个datatable 就有几个sheet
    using System;
    using System.Data;
    using Excel;
    using System.Reflection; 
    using System.Runtime.InteropServices;
    using System.IO;
    using System.Web;
    using System.Diagnostics;
    namespace HLDXS.NET.Comm
    {
    /// <summary>
    /// excel_down 的摘要说明。
    /// </summary>
    public class excel_down:System.IDisposable
    {
    DataSet Excel_DS;

    public excel_down(DataSet Excel_DS)
    {
    this.Excel_DS=Excel_DS;
    }
    public void Export_Excel(System.Web.UI.Page excel)
    {
    Excel.Application MyApp;
    if(this.Excel_DS.Tables.Count<1)
    {
    return;
    }
    int table_count=this.Excel_DS.Tables.Count;
    object oMissiong = System.Reflection.Missing.Value; MyApp=new Excel.ApplicationClass(); Workbooks workbooks = MyApp.Workbooks;
    _Workbook MyBook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);

    Excel.Sheets mysheet=null;
    mysheet=MyApp.Worksheets;

    for(int i=0;i<table_count;i++)
    {
    Excel._Worksheet MySheet1=new Excel.WorksheetClass();
    mysheet.Add(oMissiong,oMissiong,1,oMissiong);
    }
    int Columns=1;
    Excel._Worksheet MySheets=null;
    for(int i=0;i<table_count;i++)
    {
    MySheets=(Excel._Worksheet)MyBook.Worksheets.get_Item(i+1);
    MySheets.Name=this.Excel_DS.Tables[i].TableName; MySheets.Cells[1,Columns]=this.Excel_DS.Tables[i].TableName;

    for(int k=0;k<this.Excel_DS.Tables[i].Columns.Count;k++)
    {
    MySheets.Cells[2,Columns+k]=this.Excel_DS.Tables[i].Columns[k].Caption;
    for(int j=0;j<this.Excel_DS.Tables[i].Rows.Count;j++)
    {
    MySheets.Cells[j+3,Columns+k]=this.Excel_DS.Tables[i].Rows[j][k];
    }             
    }
    MySheets.get_Range(MySheets.Cells[1,Columns],MySheets.Cells[1,Columns+Excel_DS.Tables[i].Columns.Count-1]).Merge(0);
    MySheets.get_Range(MySheets.Cells[1,Columns],MySheets.Cells[1,Columns+Excel_DS.Tables[i].Columns.Count-1]).Font.Size=15;
    MySheets.get_Range(MySheets.Cells[1,Columns],MySheets.Cells[1,Columns+Excel_DS.Tables[i].Columns.Count-1]).Font.Bold=true;
    MySheets.get_Range(MySheets.Cells[1,Columns],MySheets.Cells[1,Columns+Excel_DS.Tables[i].Columns.Count-1]).RowHeight=24;
    MySheets.get_Range(MySheets.Cells[1,Columns],MySheets.Cells[1,Columns+Excel_DS.Tables[i].Columns.Count]).Borders.Weight=(OWC.LineWeightEnum.owcLineWeightThin);
    MySheets.get_Range(MySheets.Cells[2,Columns],MySheets.Cells[2,Columns+Excel_DS.Tables[i].Columns.Count]).ColumnWidth=10;
    }
    //删除第一个sheet
    int tt=MyApp.Worksheets.Count;
    MySheets=new Excel.WorksheetClass();
    MySheets=(Excel._Worksheet)MyBook.Worksheets.get_Item(tt);
    MySheets.Delete();
    MySheets=null;
    //

    string filename=excel.MapPath("")+"/"+this.Excel_DS.DataSetName+".xls";
    MySheets=new Excel.WorksheetClass();
    MySheets=(Excel._Worksheet)MyBook.Worksheets.get_Item(1);
    MyApp.Visible=false;

    MySheets.SaveAs(filename,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);
    MyApp.Workbooks.Close();
    MyApp.Quit();
    MyApp=null;
    GC.Collect();
    HttpResponse resp;
    resp =excel.Response;
    resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
    string DownFileName=this.Excel_DS.DataSetName+".xls";
    resp.AppendHeader("Content-Disposition", "attachment;filename=" +DownFileName); 
    FileInfo MyFileInfo;
    long StartPos = 0, FileSize;
    MyFileInfo = new FileInfo(filename);
    FileSize = MyFileInfo.Length; 
    resp.WriteFile(filename, StartPos, FileSize);
    resp.Flush();
    MyFileInfo.Delete();
    excel.Response.End();
    }
    public void Dispose()
    {
    this.Excel_DS=null;
    }
    }
    }
      

  10.   

    感谢: slon3dmax(slon3dmax)顶一下。
      

  11.   

    FileInfo MyFileInfo;
    long StartPos = 0, FileSize;
    MyFileInfo = new FileInfo(filename);
    FileSize = MyFileInfo.Length; 
    resp.WriteFile(filename, StartPos, FileSize);
    resp.Flush();
    MyFileInfo.Delete();
    excel.Response.End();