2017-06-08 2 views
0

Je lis dans plusieurs champs d'une feuille Excel en utilisant Interop et il fonctionne presque parfaitement. Chaque champ est lu à l'exception du champ date, qui est rempli par un nombre bizarre qui n'a rien à voir avec quoi que ce soit sur le document Excel. Mon code qui est nécessaire pour exécuter le programme est ci-dessous. Afin de voir le problème, la classe de test doit être exécutée afin de voir les résultats qui sont imprimés sur la console ainsi que les tests dans la classe de test. Le fichier Excel doit être sur le bureau et intitulé TEST.xlsx. Si vous avez besoin de vérifier ma feuille Excel, il est lié here sur un document google.Pourquoi ma classe de lecteurs Excel lit-elle correctement tous les champs, à l'exception du champ de 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> 
    /// 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; 
     } 
    } 
} 

Répondre

1

Ce nombre bizarre est en réalité juste une représentation de la date dans un format différent. Vous pouvez essayer d'utiliser la fonction CDate() sur ce numéro et cela devrait vous donner la date que vous recherchez. Une autre option consisterait à définir une variable explicitement définie en tant que type de date.