asp.net 读取Excel多个sheet表,放入dataset中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asp.net 读取Excel多个sheet表,放入dataset中相关的知识,希望对你有一定的参考价值。

怎么放
前提是不知道sheet名,可以依靠索引sheet表位置直接读取么

Com组件的方式读取Excel :
这种方式需要先引用 Microsoft.Office.Interop.Excel 。首选说下这种方式的优缺点
优点:可以非常灵活的读取Excel中的数据
缺点:如果是Web站点部署在IIS上时,还需要服务器机子已安装了Excel,有时候还需要为配置IIS权限。最重要的一点因为是基于单元格方式读取的,所以数据很慢。
代码如下:
DataTable GetDataFromExcelByCom(bool hasTitle = false)

OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "Excel(*.xlsx)|*.xlsx|Excel(*.xls)|*.xls";
openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
openFile.Multiselect = false;
if (openFile.ShowDialog() == DialogResult.Cancel) return null;
var excelFilePath = openFile.FileName;

Excel.Application app = new Excel.Application();
Excel.Sheets sheets;
object oMissiong = System.Reflection.Missing.Value;
Excel.Workbook workbook = null;
DataTable dt = new DataTable();

try

if (app == null) return null;
workbook = app.Workbooks.Open(excelFilePath, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong,
oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong);
sheets = workbook.Worksheets;

//将数据读入到DataTable中
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);//读取第一张表
if (worksheet == null) return null;

int iRowCount = worksheet.UsedRange.Rows.Count;
int iColCount = worksheet.UsedRange.Columns.Count;
//生成列头
for (int i = 0; i < iColCount; i++)

var name = "column" + i;
if (hasTitle)

var txt = ((Excel.Range)worksheet.Cells[1, i + 1]).Text.ToString();
if (!string.IsNullOrWhiteSpace(txt)) name = txt;

while (dt.Columns.Contains(name)) name = name + "_1";//重复行名称会报错。
dt.Columns.Add(new DataColumn(name, typeof(string)));

//生成行数据
Excel.Range range;
int rowIdx = hasTitle ? 2 : 1;
for (int iRow = rowIdx; iRow <= iRowCount; iRow++)

DataRow dr = dt.NewRow();
for (int iCol = 1; iCol <= iColCount; iCol++)

range = (Excel.Range)worksheet.Cells[iRow, iCol];
dr[iCol - 1] = (range.Value2 == null) ? "" : range.Text.ToString();

dt.Rows.Add(dr);

return dt;

catch return null;
finally

workbook.Close(false, oMissiong, oMissiong);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
app.Workbooks.Close();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;

参考技术A 你可以看一下这个
使用OleDb,让Excel操作与数据库一样简单

参考资料:http://www.lmwlove.com/ac/ID364

参考技术B 可以通过循环以下代码获取到每个Sheet的数据,保存到DataTable中,每次获取后,在装载到DataSet中,希望对你有用
#region 读取Excle,并将Excle中的数据保存在Datatable中
/// <summary>
/// 读取Excle,并将Excle中的数据保持在Datatable中
/// </summary>
/// <param name="filePath">要读取的文件路径</param>
/// <param name="sheetName">要读取的Excle中的工作表名</param>
/// <returns></returns>
public DataTable ImportExcelToDataTable(string filePath,string sheetName)

string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";
using (OleDbConnection conn = new OleDbConnection(strConn))

OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT*FROM[" + sheetName + "$]", strConn);
DataTable myDataTable = new DataTable();
try

myCommand.Fill(myDataTable);

catch (Exception)

return null;

return myDataTable;


#endregion
参考技术C private DataSet ExcelToDS(string Path)

string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
using (OleDbConnection conn = new OleDbConnection(strConn))


DataSet ds = new DataSet();
try

conn.Open();
string strExcel = "select * from [sheet1$],[sheet2$],[sheet3$]";
OleDbDataAdapter command = new OleDbDataAdapter(strExcel, conn);
command.Fill(ds);
conn.Close();

catch


return ds;


以上。本回答被提问者采纳
参考技术D

excel 利用正则表达式匹配工作表中的数据

excel 利用正则表达式匹配工作表中的数据

sheet1中A列放需要匹配的数据

sheet2中A列放正则表达式,可以是多个,但至少一个

匹配到了则在sheet1中C列对应行显示相关标记内容,比如本案例中显示1111

Sub Test()
atr = Worksheets("Sheet1").Range("a65536").End(xlUp).Row
btr = Worksheets("Sheet2").Range("a65536").End(xlUp).Row

a = Worksheets("Sheet1").Range("a1:a" & atr).Value
b = Worksheets("Sheet2").Range("a1:a" & btr).Value

ReDim c(1 To atr, 1 To 1)
Set reg = CreateObject("vbscript.regexp")
With reg
.Global = True
.IgnoreCase = True
For ar = 1 To atr
For br = 1 To btr
If btr = 1 Then
.Pattern = b
Else
.Pattern = b(br, 1)
End If
If .Test(a(ar, 1)) Then
c(ar, 1) = "1111"
Exit For
End If
Next
Next
End With
Range("c1:c" & atr) = c
Set reg = Nothing
End Sub

 

以上是关于asp.net 读取Excel多个sheet表,放入dataset中的主要内容,如果未能解决你的问题,请参考以下文章

ASP.net读取Excel文件的问题

ASP.NET 如何获取 Excel 中工作表的名称。

excel 利用正则表达式匹配工作表中的数据

JAVA POI导出excel:多个sheet

Python合并多个Excel工作簿

ASP.NET读取excel数据问题