用C#读取Excel文件:怎样从指定单元格开始向右向下读取数据?

Erickson 2005-04-01 11:45:10
用C#读取Excel文件时,怎样从指定单元格开始向右向下读取数据?

例如:SQL语句为:SELECT * FORM [Sheet1$] 是从工作薄1中有数据的第一个单元格开始读取数据。
那么要从指定单元格开始读取应该怎么办?
...全文
831 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
udSoft2020 2005-07-12
  • 打赏
  • 举报
回复
mark...
delphi_dcs 2005-04-02
  • 打赏
  • 举报
回复
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();
Erickson 2005-04-02
  • 打赏
  • 举报
回复
感谢: slon3dmax(slon3dmax)

顶一下。
athossmth 2005-04-01
  • 打赏
  • 举报
回复
先读到C#的datatable里,再整理数据吧
amendajing 2005-04-01
  • 打赏
  • 举报
回复
mark
pc_csharp 2005-04-01
  • 打赏
  • 举报
回复
你就rs.NextResult 多少个单元格.
或是read多少次.
:)
slon3dmax 2005-04-01
  • 打赏
  • 举报
回复
给你一个我自已写的类 只要有一个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;
}


}
}
Erickson 2005-04-01
  • 打赏
  • 举报
回复
各位大哥:

小弟已将问题解决了,拿出来共享如下:

在工作表中,用鼠标左键拖拽,选取要读取数据的区域,将其定义成为表,从菜单中选择'插入 '->'名称'->'定义',在'定义名称'对话框出现后,键入表的名字:TestTable。
在C#中定义SQL语句:SELECT * FROM TestTable,读取到DataTable中的数据就是所想得到的数据了。
Erickson 2005-04-01
  • 打赏
  • 举报
回复
全都读出来,在DataTable中处理也可以,这样必须要保证在Excel工作表中有数据的第一行必须是数据库列名。

如果我想将工作表中有数据的第二行当作数据库列名(即:从有数据的第二行单元格开始读),那么有没有好的解决方法?

能不能用在SQL语句中定位单元格呢?
chagel 2005-04-01
  • 打赏
  • 举报
回复
全都读出来又无所谓, 关键是用的时候再做选择.
所以你只要读到DataTable中去, 再选择数据就行啦
mao1997 2005-04-01
  • 打赏
  • 举报
回复
可以用office的控件操作excel
那个能指定重哪一格读取数据
Erickson 2005-04-01
  • 打赏
  • 举报
回复
对,是的。默认第一行是列名。

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

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧