我正在尋找一種將Excel工作表的某些元素導入列表的方法。我的目標是能夠對excel工作表的屬性(第一行)進行排序(單擊我想查看的屬性)並獲取第一行下面的行的值。導入Excel行,列成列表
0
A
回答
0
我會實現你想要的這種方式,而不使用Sheet
接口,但Worksheet
類對象。
有一點需要注意的是,在我獲得二維數組中所有使用的範圍之後,我正在關閉Excel表格。這使得速度更快,否則從範圍讀取會慢很多。可能有很多方法可以讓它更快。
Application xlApp = new Application();
Workbook xlWorkBook = null;
Worksheet dataSheet = null;
Range dataRange = null;
List<string> columnNames = new List<string>();
object[,] valueArray;
try
{
// Open the excel file
xlWorkBook = xlApp.Workbooks.Open(fileFullPath, 0, true);
if (xlWorkBook.Worksheets != null
&& xlWorkBook.Worksheets.Count > 0)
{
// Get the first data sheet
dataSheet = xlWorkBook.Worksheets[1];
// Get range of data in the worksheet
dataRange = dataSheet.UsedRange;
// Read all data from data range in the worksheet
valueArray = (object[,])dataRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
if (xlWorkBook != null)
{
// Close the workbook after job is done
xlWorkBook.Close();
xlApp.Quit();
}
for (int colIndex = 0; colIndex < valueArray.GetLength(1); colIndex++)
{
if (valueArray[0, colIndex] != null
&& !string.IsNullOrEmpty(valueArray[0, colIndex].ToString()))
{
// Get name of all columns in the first sheet
columnNames.Add(valueArray[0, colIndex].ToString());
}
}
}
// Now you have column names or to say first row values in this:
// columnNames - list of strings
}
catch (System.Exception generalException)
{
if (xlWorkBook != null)
{
// Close the workbook after job is done
xlWorkBook.Close();
xlApp.Quit();
}
}
相關問題
- 1. 從EPPLUS列表導出到Excel列表
- 2. 導入有2000列的Excel
- 3. SharePoint列表導出到Excel
- 4. 將從excel導入的數據幀列轉換爲列表
- 5. 導入包列表
- 6. 執行的導入函數列表
- 7. 導入CSV到Excel - 自動「文本到列」和「插入表」
- 8. 從Excel導入2列或更多列到1列訪問
- 9. SSIS 2008,Excel 2007正確格式化excel列(導出,不導入)
- 10. 從Excel導入SSIS選擇性列
- 11. SSIS excel目標 - 導入列升序
- 12. 將Excel數據導入C#時,第一行不會變成列名稱?
- 13. 用C#列表導入XML#
- 14. Python:將URL導入列表
- 15. Excel VBA導出2列列表框以分離工作表
- 16. 將C#列表導出到Excel
- 17. 從excel導入一列到python並運行自相關
- 18. EXCEL文檔單獨列成行
- 19. 需要讀取excel表C#並導入到mySQL只有3列
- 20. 如何導入所需列的工作表? Excel VBA
- 21. 將Excel電子表格列導入SQL Server數據庫
- 22. 比較從excel中導入的2個Python unicode列表
- 23. 導入Excel工作表到Mongodb數據庫與列映射
- 24. 在列表中導入SharePoint 2010 Excel庫數據
- 25. 使用jxl.jar從Excel導入不適用於列表值
- 26. 在SQL查詢中使用列表將數據導入Excel
- 27. 列導入列表到列表視圖winforms C#
- 28. 在SQL導入'NULL'行中導入excel
- 29. Excel表到列表
- 30. SSIS從excel中導入第一列的列名稱
您是否添加了excel作爲參考? – BugFinder
可能的重複[我得到「缺少一個使用指令或程序集引用」,並沒有線索發生了什麼問題](https://stackoverflow.com/questions/17344295/im-getting-the-missing-a-using -directive-or-assembly-reference-and-no-clue-wh) – SeM
你不需要寫'Excel.Sheets'。你已經使用了指令'Excel',所以只需要寫一個接口的'Sheet'即可。否則,您可以使用具體的類「工作表」。 – praty