2017-02-06 69 views
0

這是我第一次提問。所以請溫柔對我:)C#Winform Excel通過查詢讀取

我的問題是,我想格式化我們的時間和考勤終端生成的Excel文件。這裏是生成excel文件和我的問題的一些介紹:

enter image description here

我想要做的是讀取使用C#WinForm的是excel文件,並格式化的方式方法,我需要它。

我的問題是:

  • 我如何選擇表?我知道它只有一張紙,但我不知道如何使用OleDbConnection指向一張紙。 OleDBConnection中的示例是「[Sheet1 $]」來讀取工作表,但我不確定從終端生成的工作表名稱。我們可以使用索引嗎?例如:「來自[Sheet1 &]」將從「[0 $]」變爲「?」?
  • 與第一個問題相同,但在excel列中。我該如何處理它,例如,「[0],1」。
  • 最後的問題,可能會解釋一切。我真正想要做的是使用OleDbConnection,我的命令將如下所示:

    「SELECT DISTINCT [0 $],Convert([1 $],Date),MIN(Convert([1 $]時間)),MAX(轉換([$ 1],時間))FROM [0 $] GROUP BY [0 $],轉換([$ 1],日期)」

注:0 $]和[1 $]是列或表格的索引

我需要的是生成一個文件,該文件將顯示將用日期格式化的員工出勤情況,以及那裏的第一個時間IN並有最後一次超時。請查看下面的圖片瞭解輸出的想法。

enter image description here

我試圖尋找,但我沒能找到,將在配合什麼,我需要一個解決方案。

希望任何人都可以幫助我。謝謝!

回答

0

對於問題:

  • Q1:請參見代碼說明,我沒有辦法來解決這個問題,如果任何人有請分享給我。
  • Q2:與第一個相同,我沒有辦法在SQL中使用DateTableName(or Sheet Name)Columns索引。
  • Q3:使用Linq。更多關於LINQ的樣本https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

這些步驟是如何得到預期的數據:

  1. 閱讀從Excel文件中的所有數據需要知道Sheet Name;
  2. 過濾無效數據,因爲有很多列值是empty,nullspace-string但添加到行;使用Linq
  3. 查詢數據。

希望我的回答可以幫助你。

參考代碼:

 

    public void Test() 
    { 
     string filePath = $"{Environment.CurrentDirectory}//test20170206.xls"; 
     string conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1;'"; 
     //connection excel 
     OleDbConnection conn = new OleDbConnection(conStr); 
     conn.Open(); 
     //get all sheel from excel file 
     DataTable tb = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
     foreach (DataRow row in tb.Rows) 
     { 
      Console.WriteLine(row["TABLE_NAME"]); 
      //using index in row 
      for (int i=0;i p.Datetime).ToLongTimeString(), TimeMax = g.Max(p => p.Datetime).ToLongTimeString() }; 


    } 

    public class Model20170206 
    { 
     public int Id { get; set; } 
     public DateTime Datetime { get; set; } 
     public string Value1 { get; set; } 
     public string Value2 { get; set; } 
     public string Value3 { get; set; } 
     public string Value4 { get; set; } 
    } 

實踐步驟:

  • 我使用一些測試數據是這樣的:
 
1,2017-01-28 03:47:54,1,1,1,0 
2,2017-01-29 04:58:18,1,0,2,0 
3,2017-01-28 08:44:43,1,1,3,0 
4,2017-01-28 05:47:56,1,0,4,0 
0,2017-02-05 12:12:53,1,1,5,0 
1,2017-01-31 12:02:24,1,0,6,0 
2,2017-02-05 12:30:34,1,1,7,0 
3,2017-02-04 02:30:08,1,0,8,0 
4,2017-02-01 11:39:53,1,1,9,0 
0,2017-02-05 07:45:58,1,0,10,0 
1,2017-02-05 03:01:46,1,1,11,0 
2,2017-02-02 09:22:17,1,0,12,0 
3,2017-01-30 03:05:46,1,1,13,0 
4,2017-02-04 09:02:21,1,0,14,0 
0,2017-02-03 07:58:20,1,1,0,0 
1,2017-01-29 07:53:48,1,0,1,0 
2,2017-01-29 12:41:25,1,1,2,0 
3,2017-02-06 02:58:50,1,0,3,0 
4,2017-01-28 11:06:47,1,1,4,0 
0,2017-02-04 10:40:18,1,0,5,0 
1,2017-01-31 12:57:24,1,1,6,0 
2,2017-02-03 12:28:38,1,0,7,0 
3,2017-02-01 06:48:23,1,1,8,0 
4,2017-01-28 12:42:59,1,0,9,0 
0,2017-02-04 10:34:44,1,1,10,0 
1,2017-02-06 06:38:31,1,0,11,0 
2,2017-02-05 08:40:26,1,1,12,0 
3,2017-01-31 01:56:32,1,0,13,0 
4,2017-02-05 11:13:11,1,1,14,0 
0,2017-01-29 10:34:58,1,0,0,0 
1,2017-02-02 04:01:18,1,1,1,0 
2,2017-02-06 01:08:09,1,0,2,0 
3,2017-01-28 07:24:11,1,1,3,0 
4,2017-02-02 08:25:50,1,0,4,0 
0,2017-01-28 08:01:13,1,1,5,0 
1,2017-02-03 08:33:10,1,0,6,0 
2,2017-01-29 03:47:03,1,1,7,0 
3,2017-02-05 12:36:56,1,0,8,0 
4,2017-02-04 06:10:55,1,1,9,0 
0,2017-01-29 05:13:43,1,0,10,0 
1,2017-02-06 06:35:18,1,1,11,0 
2,2017-01-31 08:23:25,1,0,12,0 
3,2017-02-03 04:25:10,1,1,13,0 
4,2017-01-31 04:31:34,1,0,14,0 
0,2017-01-30 10:03:42,1,1,0,0 
1,2017-01-30 11:07:57,1,0,1,0 
2,2017-02-05 11:17:45,1,1,2,0 
3,2017-02-02 12:59:56,1,0,3,0 
4,2017-01-31 04:49:48,1,1,4,0 
0,2017-02-02 01:02:05,1,0,5,0 
1,2017-01-31 11:16:52,1,1,6,0 
2,2017-02-03 09:53:51,1,0,7,0 
3,2017-01-31 04:02:09,1,1,8,0 
4,2017-01-28 05:06:38,1,0,9,0 
0,2017-01-28 09:18:28,1,1,10,0 
1,2017-02-01 04:26:56,1,0,11,0 
2,2017-02-03 11:17:34,1,1,12,0 
3,2017-02-06 09:09:23,1,0,13,0 
4,2017-01-30 08:20:51,1,1,14,0 
0,2017-02-05 06:42:01,1,0,0,0 
1,2017-02-01 04:29:38,1,1,1,0 
2,2017-02-02 05:28:21,1,0,2,0 
3,2017-02-05 04:24:37,1,1,3,0 
4,2017-01-31 07:59:14,1,0,4,0 
0,2017-01-31 10:38:59,1,1,5,0 
1,2017-02-06 03:01:17,1,0,6,0 
2,2017-02-02 08:52:25,1,1,7,0 
3,2017-02-02 07:47:35,1,0,8,0 
4,2017-02-04 07:55:27,1,1,9,0 
0,2017-02-06 12:32:50,1,0,10,0 
1,2017-01-28 11:01:04,1,1,11,0 
2,2017-01-30 01:12:09,1,0,12,0 
3,2017-01-29 03:42:15,1,1,13,0 
4,2017-02-05 07:00:44,1,0,14,0 
0,2017-02-05 05:12:40,1,1,0,0 
1,2017-01-28 11:28:18,1,0,1,0 
2,2017-02-05 09:11:08,1,1,2,0 
3,2017-01-29 09:11:08,1,0,3,0 
4,2017-02-04 03:46:57,1,1,4,0 
0,2017-02-02 06:21:06,1,0,5,0 
1,2017-01-28 02:15:06,1,1,6,0 
2,2017-01-31 02:34:50,1,0,7,0 
3,2017-02-05 10:14:23,1,1,8,0 
4,2017-01-31 05:05:26,1,0,9,0 
0,2017-02-05 12:25:46,1,1,10,0 
1,2017-02-05 07:15:07,1,0,11,0 
2,2017-02-03 04:00:19,1,1,12,0 
3,2017-02-06 03:25:13,1,0,13,0 
4,2017-02-02 06:01:42,1,1,14,0 
0,2017-02-03 04:41:57,1,0,0,0 
1,2017-02-06 10:23:15,1,1,1,0 
2,2017-01-29 07:45:19,1,0,2,0 
3,2017-02-03 11:10:25,1,1,3,0 
4,2017-02-05 12:36:33,1,0,4,0 
0,2017-02-03 01:51:44,1,1,5,0 
1,2017-02-01 08:01:16,1,0,6,0 
2,2017-02-01 05:06:28,1,1,7,0 
3,2017-01-31 03:20:15,1,0,8,0 
4,2017-02-05 07:00:14,1,1,9,0 
0,2017-01-31 08:10:04,1,0,10,0 
1,2017-02-03 03:23:12,1,1,11,0 
2,2017-01-31 10:24:29,1,0,12,0 
3,2017-02-04 06:12:08,1,1,13,0 
4,2017-01-31 05:24:38,1,0,14,0 
0,2017-02-01 08:55:20,1,1,0,0 
1,2017-01-29 04:59:03,1,0,1,0 
2,2017-02-02 01:05:55,1,1,2,0 
3,2017-02-01 10:09:05,1,0,3,0 
4,2017-01-30 01:01:37,1,1,4,0 
0,2017-02-06 10:57:49,1,0,5,0 
1,2017-02-01 08:23:28,1,1,6,0 
2,2017-02-01 09:00:45,1,0,7,0 
3,2017-01-30 12:16:46,1,1,8,0 
0,2017-02-04 06:06:36,1,0,10,0 
1,2017-02-01 09:31:02,1,1,11,0 
... 
more 
... 
  • 導入到Excel文件,如下圖所示:

enter image description here

  • 我的代碼的結果數據是這樣的: enter image description here