![](/img/trans.png)
[英]using Excel = Microsoft.Office.Interop.Excel Compilation Error
[英]Excel to DataTable? Without using Microsoft.Office.Interop.Excel
我們的計算機上未安裝Microsoft Office。 我正在嘗試讀取Excel文件並將其存儲在數據集中,但是在
Dim ws As Microsoft.Office.Interop.Excel.Worksheet
Dim Obj As Object
ws = DirectCast(workbook.Worksheets(1), Microsoft.Office.Interop.Excel.Worksheet) <---error here
錯誤: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154.
我知道它很可能在計算機上沒有安裝Microsoft Office。 但是還有其他選擇嗎? 我嘗試了Epplus,但是Internet上有關Epplus的大多數代碼都在C#中進行,因此我無法使用開發人員融合或telerik將其轉換為vb。
可以請一個簡單的代碼片段或將Excel數據放入DataSet的內容嗎? 謝謝。
編輯:為NPOI添加一些示例代碼。
public static DataTable ExcelInput(string FilePath)
{
DataTable table = new DataTable();
//Get the excel from filepath
HSSFWorkbook workbook = new HSSFWorkbook(File.Open(FilePath, FileMode.Open));
HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0);
//get Excel rows
int rowsCount = sheet.PhysicalNumberOfRows;
int colsCount = sheet.GetRow(0).PhysicalNumberOfCells;
for (int i = 0; i < colsCount; i++)
{
table.Columns.Add(i.ToString());
}
for (int x = 0; x < rowsCount; x++)
{
DataRow dr = table.NewRow();
for (int y = 0; y < colsCount; y++)
{
dr[y] = sheet.GetRow(x).GetCell(y).ToString();
}
table.Rows.Add(dr);
}
sheet = null;
workbook = null;
return table;
}
確保在未安裝excel的情況下調用Excel互操作時會出錯!
但是,讓您開始吧: 使用VB.NET和ASP.NET創建Excel工作簿
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.