是否有可能通过内存从Excel导入数据?

假设打开一个新的Excel文件并input数据。 外部的C#应用​​程序可以挂钩到Excel进程并从中获取数据吗? 而且,Excel文件从不保存到文件中。

这难道不能做到吗?还可以指导我如何实现这一目标的正确方向。

只需将该代码添加到添加了名为“button1”的button的窗体应用程序的代码后面。 此示例代码将遍历Excel文件的单元格,并在消息框中显示每个单元格的内容。 当然,需要“Microsoft.Office.Interop.Excel”的项目引用。

并且不要忘了实际创buildexcel文件,在这个例子中我使用了这个path“C:\ ExcelData \ DataToImp.xlsx”。

using Excel = Microsoft.Office.Interop.Excel; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; Excel.Range range; string str; int rCnt = 0; int cCnt = 0; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open("C:\\ExcelData\\DataToImp.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); range = xlWorkSheet.UsedRange; for (int rowIndex = 1; rowIndex <= range.Rows.Count; rowIndex++) { for (int columnIndex = 1; columnIndex <= range.Columns.Count; columnIndex++) { Excel.Range rangeTest = range.Cells[rowIndex, columnIndex] as Excel.Range; if (rangeTest.MergeCells)// && rangeTest.Value2 == null) { //continue; int columns = rangeTest.Columns.Count; columnIndex += columns; } MessageBox.Show("m " + (string)rangeTest.Value2); } } xlWorkBook.Close(true, null, null); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); } private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; MessageBox.Show("Unable to release the Object " + ex.ToString()); } finally { GC.Collect(); } }