Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的excel reader类能够正确读取除日期字段以外的所有字段?_C#_Excel_Visual Studio_Interop - Fatal编程技术网

C# 为什么我的excel reader类能够正确读取除日期字段以外的所有字段?

C# 为什么我的excel reader类能够正确读取除日期字段以外的所有字段?,c#,excel,visual-studio,interop,C#,Excel,Visual Studio,Interop,我正在使用Interop从excel工作表中读取多个字段,它工作得几乎完美。除日期字段外,所有字段都被读入,该字段由一个奇怪的数字填充,与excel文档上的任何内容都无关。下面是我运行该程序所需的代码。为了查看问题,必须运行测试类,以便查看控制台上打印的结果以及测试类中的测试。excel文件必须位于桌面上,标题为TEST.xlsx。如果你需要查看我的Excel表格,它会链接到谷歌文档上 using System; using System.Collections.Generic; using S

我正在使用Interop从excel工作表中读取多个字段,它工作得几乎完美。除日期字段外,所有字段都被读入,该字段由一个奇怪的数字填充,与excel文档上的任何内容都无关。下面是我运行该程序所需的代码。为了查看问题,必须运行测试类,以便查看控制台上打印的结果以及测试类中的测试。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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Microsoft.Office.Interop.Excel;
使用_Excel=Microsoft.Office.Interop.Excel;
命名空间控制台应用程序1
{
/// 
///此类从excel工作表读取数据,并将数据存储在Sales对象中
///然后将每次销售放入销售列表中。
/// 
公共类阅读器
{
公共字符串路径;
公共应用程序excel;
公共工作手册wb;
公共服务(ws);;
公共列表salesList{get;set;}
/// 
///此构造函数打开excel文件,创建销售列表,创建修改器
///反对。
/// 
///要打开的excel文件的名称。
///excel文件中的图纸编号。
公共读取器(字符串路径,整型工作表)
{
this.path=path;
excel=new_excel.Application();
wb=excel.Workbooks.Open(路径);
ws=wb.工作表[工作表];
salesList=新列表();
createSales();
//控制台输出测试,以确保正确读取excel文件。
for(int i=0;i