我正在尋找一種將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. 在Python列表中導入excel列
- 2. 從excel導入列表,但無法將其分成4列
- 3. 如何將SharePoint列表導入Excel VBA?
- 4. 導入Excel colums到列表框
- 5. 試圖從Sharepoint導入列表到Excel
- 6. 在Sybase表中導入excel列
- 7. 將列表插入Excel列
- 8. 從EPPLUS列表導出到Excel列表
- 9. 導入有2000列的Excel
- 10. 導入列表
- 11. SharePoint列表導出到Excel
- 12. 生成Java文件的導入列表
- 13. 將excel數據導入SharePoint列表中的查找列
- 14. 將從excel導入的數據幀列轉換爲列表
- 15. 導入包列表
- 16. 導入SQL列表
- 17. 將列插入導入表
- 18. SQL Server導入行字符串表列
- 19. 執行的導入函數列表
- 20. 導入CSV到Excel - 自動「文本到列」和「插入表」
- 21. 從Excel導入2列或更多列到1列訪問
- 22. SSIS 2008,Excel 2007正確格式化excel列(導出,不導入)
- 23. 使用VBA從Excel表導入Sharepoint 2010列表數據
- 24. 將Excel工作表數據導入到Sharepoint列表的工具
- 25. Excel導入CSV添加附加列
- 26. 從Excel導入SSIS選擇性列
- 27. 從excel導入數據 - 。 (點)列名
- 28. SSIS excel目標 - 導入列升序
- 29. 導出datagridview列excel
- 30. 將Excel數據導入C#時,第一行不會變成列名稱?
您是否添加了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