2017-06-08 4 views
0

Interopを使用してExcelシートから複数のフィールドを読み込んでいて、ほぼ完全に動作しています。日付フィールド以外のすべてのフィールドが読み込まれます。日付フィールドには、Excelドキュメント上の何も関係のない奇妙な数字が入力されます。プログラムを実行するために必要な私のコードは以下の通りです。問題を見るためには、コンソールに出力された結果とテストクラスのテストを確認するためにテストクラスを実行する必要があります。 Excelファイルは、デスクトップにあり、TEST.xlsxというタイトルでなければなりません。私のExcelシートをチェックする必要がある場合は、hereがGoogleドキュメントにリンクされています。私の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; 
     } 
    } 
} 

答えて

1

実際には、奇妙な数字は、実際には異なる形式の日付を表しています。その番号にCDate()関数を使用してみると、探している日付が表示されます。もう1つのオプションは、日付タイプとして明示的に定義された変数に設定することです。

関連する問題