为什么我的excel阅读器类除了date字段之外正确地读取每个字段?

我在使用Interop的Excel表格中阅读了多个领域,并且工作得几乎完美。 除了date字段外,每个字段都被读入,而date字段由一个奇怪的数字填充,这个数字与Excel文档中的任何内容无关。 下面是我需要运行程序的代码。 为了查看问题,必须运行testing类才能看到在控制台上输出的结果以及testing类中的testing。 excel文件必须在桌面上,标题为TEST.xlsx。 如果您需要检查我的Excel工作表,它链接在这里的谷歌文件。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Office.Interop.Excel; using _Excel = Microsoft.Office.Interop.Excel; namespace ConsoleApplication1 { /// <summary> /// This class reads data from an excel sheet and stores the data in Sales objects /// and then places each Sale into a List of Sales. /// </summary> public class Reader { public string path; public _Application excel; public _Workbook wb; public _Worksheet ws; public List<Sales> salesList { get; set; } /// <summary> /// This constructor opens the excel file, creates a list of sales, creates a modifier /// object. /// </summary> /// <param name="path"> Name of the excel file to be opened. </param> /// <param name="sheet"> Sheet number within the excel file. </param> public Reader(string path, int sheet) { this.path = path; excel = new _Excel.Application(); wb = excel.Workbooks.Open(path); ws = wb.Worksheets[sheet]; salesList = new List<Sales>(); createSales(); // Console output test to ensure the excel file is being read correctly. for (int i = 0; i < salesList.Count; i++ ) { Console.WriteLine("Row: " + (i + 1).ToString()); Console.WriteLine(salesList[i].salesNum); Console.WriteLine(salesList[i].material); Console.WriteLine(salesList[i].description); Console.WriteLine(salesList[i].MSPS); Console.WriteLine(salesList[i].MRPC); Console.WriteLine(salesList[i].quantity); Console.WriteLine(salesList[i].date); Console.WriteLine(""); } } public Reader() { new Reader("", 1); } /// <summary> /// This method creates a new Sale for every row in the excel file. /// </summary> /// <returns> Number of rows (sales) in the excel sheet. </returns> public int createSales() { int rows = 1; // Excel sheets start at 1 not 0. while (ws.Cells[rows, 1].Value2 != null) { Sales sale = new Sales(); addFields(sale, rows); salesList.Add(sale); rows++; } return rows; } /// <summary> /// This helper method adds fields to all of the sales. /// </summary> /// <param name="sale"> Sale that is getting fields filled. </param> /// <param name="row"> Row to look for fields on </param> private void addFields(Sales sale, int row) { int i = 1; sale.salesNum = readCell(row, i); // Sales Number field i++; sale.material = readCell(row, i); // material field i++; sale.description = readCell(row, i); // Description field i++; sale.MSPS = readCell(row, i); // MSPS field i++; sale.MRPC = readCell(row, i); // MRPC field i++; sale.quantity = readCell(row, i); // Quantity field i++; sale.date = readCell(row, i); // Date field } /// <summary> /// This method reads a cell from the excel document. /// </summary> /// <param name="i"> The x-coordinate of the cell to read. </param> /// <param name="j"> The y-coordinate of the cell to read. </param> /// <returns> Data in the cell in a string. </returns> private string readCell(int i, int j) { if (ws.Cells[i, j].Value2 != null) { return ws.Cells[i, j].Value2.ToString(); } else { return ""; } } } } using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections; using System.Collections.Generic; using ConsoleApplication1; namespace AutomationProgramTests { [TestClass] public class ReaderTest { Reader reader; [TestMethod] public void testCreateSales() { reader = new Reader(@"C:\Users\abochel\Desktop\TEST.xlsx", 1); // Check if the list added every sale. Assert.AreEqual(90, reader.salesList.Count); // Check contents of sales[0]. Assert.AreEqual("5/11/2017", reader.salesList[0].date); // Check contents of sales[1] Assert.AreEqual("5/11/2017", reader.salesList[1].date); // Check contents of sales[89] Assert.AreEqual("5/22/2017", reader.salesList[0].date); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Office.Interop.Excel; using _Excel = Microsoft.Office.Interop.Excel; namespace ConsoleApplication1 { /// <summary> /// Created and tested by Alexander James Bochel. /// Last Updated: 6/7/2017 /// </summary> class Program { /// <summary> /// This will call the rest of the classes in the program. /// </summary> /// <param name="args"> Command line arguments. </param> static void Main(string[] args) { openAndExecute(); } public static void openAndExecute() { Reader reader = new Reader(@"C:\Users\abochel\Desktop\TEST.xlsx", 1); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { /// <summary> /// This class contains information about each individual row on the excel sheet. /// </summary> public class Sales { // Each variable is a cell in the row for each sales order in excel. public String salesNum { get; set; } public String material { get; set; } public String description { get; set; } public String MSPS { get; set; } public String MRPC { get; set; } public String quantity { get; set; } public String date { get; set; } /// <summary> /// Basic Constructor. /// </summary> public Sales() { // TODO finish basic constructor. } /// <summary> /// This constructor sets up all of the variables within each sale. /// </summary> /// <param name="salesN"> Sales number </param> /// <param name="mat"> Type of material </param> /// <param name="desc"> Description </param> /// <param name="MS"> IDK </param> /// <param name="MR"> IDK </param> /// <param name="quant"> How many </param> /// <param name="dat"> IDK </param> public Sales(String salesN, String mat, String desc, String MS, String MR, String quant, String dat) { // Can these be deleted. salesNum = salesN; material = mat; description = desc; MSPS = MS; MRPC = MR; quantity = quant; date = dat; } } } 

奇怪的数字实际上只是以不同的格式表示date。 您可以尝试使用该号码的CDate()函数,并应该给你你正在寻找的date。 另一种select是将其设置为明确定义为datetypes的variables。