使用C#将Excel工作表的内容写入控制台时,输出不是人类可读的格式

我正在尝试读取Excel表格并显示内容到控制台。 代码在这里

FileStream fileStream = new FileStream(@"E:\USERS\MyWorkbook.xlsx", FileMode.Open, FileAccess.Read); byte[] byteCode = new byte[fileStream.Length]; fileStream.Read(byteCode, 0, (int)fileStream.Length); foreach (var byteValue in byteCode) { Console.Write(Convert.ToChar(byteValue)); } Console.ReadLine(); 

输出是

PK♥♦¶♠! bî?h ^???[Content_Types] .xml??(?

¬?ENA0►E÷HüCä-Jܲ5í?Ç↕* Q> AA?ƪc[?iiÿ??û►B¡§j7±↕IÜ{2ñIh²nm¶??Æ»R♀?? EAU ^←7 /> AC %¿↨↓?rZYï¶←@ 1↓__?f? q·AR→h h h a a R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R R 3▬§§↔»»»((((►►►

它出什么问题了?

在Oledb(对象链接和embedded数据库)的帮助下。 OLE DB是Microsoft的战略性低级应用程序接口(API),用于访问不同的数据源。

OLE DB中的对象主要由数据源对象,会话对象,命令对象和行集对象组成。 使用OLE DB的应用程序将使用此请求序列:

  1. 初始化OLE。
  2. 连接到数据源。
  3. 发出命令。
  4. 处理结果。
  5. 释放数据源对象并取消初始化OLE。

这里是以前的stackoverflow答案如何使用olddb从Excel中访问数据的链接。

如何使用c#从excel文件读取数据

这里是示例代码

 string path = @"E:\USERS\MyWorkbook.xlsx"; //Create connection string to Excel work book string excelConString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False"; OleDbConnection excelCon = new OleDbConnection(excelConString); excelCon.Open(); DataTable dtsheet = new DataTable(); dtsheet = excelCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); foreach (DataRow row in dtExcelSheet.Rows) { Query = string.Format("Select * from [{0}]", row["TABLE_NAME"].ToString()); //Create OleDbCommand to fetch data from Excel OleDbCommand cmd = new OleDbCommand(Query, excelCon); DataSet ds = new DataSet(); OleDbDataAdapter oda = new OleDbDataAdapter(Query, excelCon); excelCon.Close(); oda.Fill(ds); DataTable Exceldt = ds.Tables[0]; foreach (DataRow dr in Exceldt.Rows) { //code to display } } 

您可能应该查看办公互操作对象模型或使用oledb适配器。

Codeproject在这里的对象模型有一些很好的资源http://www.codeproject.com/Tips/696864/Working-with-Excel-using-Csharp

有一个关于如何使用oledb连接来查询数据在此之前的stackoverflow答案https://stackoverflow.com/a/16051/6080982

办公室对象的一些示例代码:

 using Excel = Microsoft.Office.Interop.Excel; Excel.Application app = new Excel.Application(); Excel.Workbook wb = app.Workbooks.Open("path to a workbook"); Excel.Worksheet sheet = (Excel.Worksheet)wb.Sheets[1]; int lastrow = sheet.UsedRange.Rows.Count; int lastcol = sheet.UsedRange.Rows.Count; Excel.Range c1; Excel.Range c2; for (int i = 1; i <= lastrow; i++) { c1 = (Excel.Range)sheet.Cells[i, 1]; c2 = (Excel.Range)sheet.Cells[i, lastcol]; Excel.Range range = sheet.Range[c1, c2]; foreach(Object o in (System.Array)range.Value) { if (o != null) { Console.Write(o.ToString()); } } Console.WriteLine(); } Console.ReadLine(); 

像其他人所说的,你应该看看办公室的互操作对象。 添加Microsoft.Office.Interop.Excel作为参考到您的项目,你通常可以find这个DLL在

 C:\Program Files (x86)\Microsoft Visual Studio 12.0\Visual Studio Tools for Office\PIA\Office15 

这里有一个代码可以帮助你做你想做的事情,我已经添加了评论来帮助你更好地理解每一行:

 Excel.Application xlApp = new Excel.Application(); //Points to the excel path Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"E:\USERS\MyWorkbook.xlsx"); //Sheets[1] = First worksheet, modify this according to your need Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1]; //UsedRange means the range of cells that has contents (are being used). Excel.Range xlRange = xlWorksheet.UsedRange; int rowCount = xlRange.Rows.Count; int colCount = xlRange.Columns.Count; for (int i = 1; i <= rowCount; i++) { for (int j = 1; j <= colCount; j++) { var cellContent = xlWorksheet.Cells[i, j].Value; //To prevent exceptions due to null reference if (cellContent != null) { Console.WriteLine(cellContent.ToString()); } } } 

首先使用.UsedRange来计算包含东西的最大行数和列数。 然后你只需要做一个嵌套循环,并使用Console.WriteLine显示每个单元格中的内容(就像你想要的那样)。