除了日期字段之外,为什么我的Excel阅读器类正确读取每个字段?

时间:2017-06-08 18:41:32

标签: c# excel visual-studio interop

我正在使用Interop从excel表中读取多个字段,它几乎完美地工作。除了日期字段之外,每个字段都被读入,日期字段由与excel文档上的任何内容无关的奇怪数字填充。运行程序所需的代码如下所示。为了查看问题,必须运行测试类,以便查看在控制台上打印的结果以及测试类中的测试。 excel文件必须位于桌面上,标题为TEST.xlsx。如果您需要检查我的Excel工作表,则会在Google文档上链接here

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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这个奇怪的数字实际上只是以不同格式表示日期。您可以尝试在该号码上使用CDate()功能,该功能应该为您提供所需的日期。另一种选择是将其设置为明确定义为日期类型的变量。