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。
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());
}
这里有一些操作Excel的资源
http://www.cnblogs.com/downmoon/archive/2008/08/20/1272185.html