首页 新闻 会员 周边 捐助

c# 如何将excell表中sheet1中的数据存到二维数组中

0
悬赏园豆:100 [已关闭问题]

c# 如何将excell表中sheet1中的数据存到二维数组中,用opendialog选择excel表.谢谢!

yanbing的主页 yanbing | 初学一级 | 园豆:50
提问于:2009-08-14 11:27
< >
分享
其他回答(2)
0

excel操作类,返回dataset:

Code

 

用opendialog取文件名,显示到textBox_FolderSelect.Text文本框中:           

 folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.Desktop;
            folderBrowserDialog1.SelectedPath = System.Environment.SpecialFolder.MyComputer.ToString();
            folderBrowserDialog1.ShowDialog();
            if (folderBrowserDialog1.SelectedPath != string.Empty)
            {
                textBox_FolderSelect.Text = folderBrowserDialog1.SelectedPath;
            }

sheet1的话要将表名写为[Sheet1$]

包括中括号,try it。

风海迷沙 | 园豆:4453 (老鸟四级) | 2009-08-14 12:13
1
using mExcel = Microsoft.Office.Interop.Excel;

string fileName = Server.MapPath("sample.xls");
if (!File.Exists(fileName))
return;

mExcel.Application mApp
= new mExcel.Application();
mExcel.Workbook mBook
= mApp.Workbooks.Open(fileName, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
mExcel.Worksheet mSheet
= mBook.Worksheets[1] as mExcel.Worksheet;

mExcel.Range mRange
= mSheet.get_Range("A1", "E12");

Array mArray
= mRange.Formula as Array;

mBook.Close(
false, null, null);
mApp.Quit();

mRange
= null;
mSheet
= null;
mBook
= null;
mApp
= null;


if (mArray != null)
{
StringBuilder sBuilder
= new StringBuilder();
int cells = mArray.GetLength(0);
int rows = mArray.GetLength(1);

for (int i = 1; i <= cells; i++)
{
for (int j = 1; j <= rows; j++)
{
sBuilder.Append(mArray.GetValue(i, j)
+ " ");
}
sBuilder.Append(
"<br/>");
}

Response.Write(sBuilder.ToString());
}

希望对你有所帮助吧!
slokra | 园豆:264 (菜鸟二级) | 2009-08-14 18:00
0

这里有一些操作Excel的资源

http://www.cnblogs.com/downmoon/archive/2008/08/20/1272185.html

邀月 | 园豆:25475 (高人七级) | 2009-08-15 09:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册