c#如何把excel数据存入一个二维数组中?

lll00ooo_Hui 2010-09-26 08:21:30
我想在用c#实现把excel数据存入一个二维数组中,并能在后台中显示出来。。
static void Main()
{
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\lenovo\\Desktop\\testshuju.xls;Extended Properties=Excel 8.0";
OleDbConnection conn = new OleDbConnection(connStr);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, connStr);
ds = new DataSet();
myCommand.Fill(ds, "table1");
}
接下来如何用Console.Write()显示出来这些数据?源程序还需要添加哪些代码?谢谢了。。
...全文
359 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lll00ooo_Hui 2010-09-26
  • 打赏
  • 举报
回复
不行啊。。
遍历dataset 赋值到数组
DataTable dt=ds.Tables[0];
foreach (DataRow dr in dt.Rows)
{}
DataRow.ItemArray
这里有点问题。。能不能再写的详细点。。
wuyq11 2010-09-26
  • 打赏
  • 举报
回复
string[,] arr= new string[row,colCount];
Excel.Range range = (Excel.Range)workSheet.Cells[top,left];
range = range.get_Resize(row,colCount);
range.Value = arr;
excel导入dataset
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
using(OleDbConnection OleConn = new OleDbConnection(strConn))
{
OleConn.Open();
String sql = "SELECT * FROM [Sheet1$]";
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
DataSet ds= new DataSet();
OleDaExcel.Fill(ds);
OleConn.Close();
}
遍历dataset 赋值到数组
DataTable dt=ds.Tables[0];
foreach (DataRow dr in dt.Rows)
{}
DataRow.ItemArray

110,539

社区成员

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

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

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