我搜索了官方文檔,但它專注於創建Excel文件而不是閱讀。任何代碼片段作爲例子都會幫助我!從Excel文件中讀取數據
如何讀取Excel數據?
COMPANY | DOCUMENT |
COMPANYONE | 123455986 |
COMPANYTWO | 123455986 |
COMPANYTHREE| 123455986 |
我搜索了官方文檔,但它專注於創建Excel文件而不是閱讀。任何代碼片段作爲例子都會幫助我!從Excel文件中讀取數據
如何讀取Excel數據?
COMPANY | DOCUMENT |
COMPANYONE | 123455986 |
COMPANYTWO | 123455986 |
COMPANYTHREE| 123455986 |
這裏是將讀取CSV和Excel數據,並通過返回的NuGet作爲數據表
注意 - 安裝 - 包ExcelDataReader
using Excel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
public static DataTable LoadDataTable(string filePath)
{
string fileExtension = Path.GetExtension(filePath);
switch (fileExtension.ToLower())
{
case ".xlsx":
return ConvertExcelToDataTable(filePath, true);
case ".xls":
return ConvertExcelToDataTable(filePath);
case ".csv":
return ConvertCsvToDataTable(filePath);
default:
return new DataTable();
}
}
public static DataTable ConvertExcelToDataTable(string filePath, bool isXlsx = false)
{
FileStream stream = null;
IExcelDataReader excelReader = null;
DataTable dataTable = null;
stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
excelReader = isXlsx ? ExcelReaderFactory.CreateOpenXmlReader(stream) : ExcelReaderFactory.CreateBinaryReader(stream);
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
if (result != null && result.Tables.Count > 0)
dataTable = result.Tables[0];
return dataTable;
}
public static DataTable ConvertCsvToDataTable(string filePath)
{
DataTable dt = new DataTable();
using (StreamReader sr = new StreamReader(filePath))
{
string[] headers = sr.ReadLine().Split(',');
foreach (string header in headers)
{
dt.Columns.Add(header);
}
while (!sr.EndOfStream)
{
string[] rows = sr.ReadLine().Split(',');
DataRow dr = dt.NewRow();
for (int i = 0; i < headers.Length; i++)
{
dr[i] = rows[i];
}
dt.Rows.Add(dr);
}
}
return dt;
}
或者使用Excel互操作的方法:
using Microsoft.Office.Interop.Excel;
...
Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = xl.Workbooks.Open(@"C:\test.xlsx");
Microsoft.Office.Interop.Excel.Worksheet sheet = workbook.Sheets[1];
int numRows = sheet.UsedRange.Rows.Count;
int numColumns = 2; // according to your sample
List<string[]> contents = new List<string[]>();
string [] record = new string[2];
for (int rowIndex = 1; rowIndex <= numRows; rowIndex++) // assuming the data starts at 1,1
{
for (int colIndex = 1; colIndex <= numColumns; colIndex++)
{
Range cell = (Range)sheet.Cells[rowIndex, colIndex];
if (cell.Value != null)
{
record[colIndex-1] = Convert.ToString(cell.Value);
}
}
contents.Add(record);
}
xl.Quit();
Marshal.ReleaseComObject(xl);
前段時間我有同樣的問題。
我使用此解決方案從ASP.NET中的Excel文件(.xls)讀取數據:http://www.dotnetcurry.com/ShowArticle.aspx?ID=138。
只需通過SQL讀取範圍並將其發佈到網頁上的GridView中即可。
它讓我更容易理解ASP.NET/C#開發者的經驗。
你想用你讀的數據做什麼?你已經嘗試了哪些方法?在任何人都可以真正回答你的問題之前,我們需要更好地瞭解你想要做的事情。 – ScruffMcGruff
只是讀取Excel文件的一個簡單示例,請參閱這個[question](http://stackoverflow.com/questions/11685204/reading-excel-spreasheet-using-epplus) – peteb
只是讀取電子表格數據,其中包含客戶將不得不諮詢這些客戶信息,沒有必要在數據庫中創建記錄。 – stringnome