c# 导入excel
时间: 2023-10-09 07:16:12 浏览: 165
在C#中导入Excel有多种方法。其中一种常用的方法是使用MyXls库来读取Excel数据。以下是一个示例代码:
```
//获取Excel路径
string fileUrl = GetExcel(fuImportContact);
//加载要导入的Excel
XlsDocument xls = new XlsDocument(fileUrl);
//获得Excel中的指定一个工作页
Worksheet sheet = xls.Workbook.Worksheets[0];
//读取数据,循环每个工作页的每一行,不读取第一行
for (int i = 2; i < sheet.Rows.Count; i++)
{
//这里进行写入数据库操作
//获取数据:sheet.Rows[ushort.Parse(i.ToString())].GetCell(1).Value.ToString()
}
```
然而,使用MyXls导出的Excel存在一个缺点,即只能读取第一列的数据。解决这个问题的方法是重新编辑一下Excel或者另存为xls格式。
如果你想在ASP.NET页面中实现上传并导入Excel,你可以使用FileUpload控件和Button控件来实现。以下是示例代码:
```
<asp:FileUpload ID="FileImport" CssClass="input" runat="server" Width="200px" />
<asp:Button ID="btnImport" runat="server" Text="确定导入" />
```
阅读全文
相关推荐














