2015年2月25日 星期三

比較資料表 Compare DataTable

比較兩個資料表,並產生出新增、刪除、修改三張資料表


private void outputDataTable(DataTable dt1,DataTable dt2,string keyword,DataTable dtDel,DataTable dtIns,DataTable dtUpd) {
   
    //<DELETE>
    //假設新資料為 1 2 4
    //舊資料為 1 2 3 4
    //把舊資料複製至C資料表
    //C資料表刪除1 2 4 剩下3
    //3 為真正要刪除的資料
    //使用者在畫面上操作的資料表為 dt2
    //資料庫查詢的原始資料表為 dt1  (原始資料)
    //將可變動資料表的 keyword 串起來
    //去原始資料表過濾,條件為NOT IN
    string strKeywordDelete = "";
    for (int i = 0; i < dt2.Rows.Count; i++)
    {
        if (i == dt2.Rows.Count - 1)
            strKeywordDelete = strKeywordDelete + "'" + dt2.Rows[i][keyword] + "'";
        else
            strKeywordDelete = strKeywordDelete + "'" + dt2.Rows[i][keyword] + "',";
    }
    if (!strKeywordDelete.Equals(""))
    {
        string _RowFilter = keyword +" NOT IN (" + strKeywordDelete + ")";
        dtDel.Clear();
        dtDel = dt1.Copy();
        dtDel.DefaultView.RowFilter = _RowFilter;
        dtDel = dtDel.DefaultView.ToTable();
    }
    if (dt2.Rows.Count == 0)
    {
        dtDel.Clear();
        dtDel = dt1.Copy();
    }

    //<INSERT>

    //將原始資料表的keyword串起來
    //去可變動資料表過濾,條件為NOT IN
    string strKeywordInsert = "";
    for (int i = 0; i < dt1.Rows.Count; i++)
    {
        if (i == dt1.Rows.Count - 1)
        {
            strKeywordInsert = strKeywordInsert + "'" + dt1.Rows[i][keyword] + "'";
        }
        else
        {
            strKeywordInsert = strKeywordInsert + "'" + dt1.Rows[i][keyword] + "',";
        }
    }
    if (!strKeywordInsert.Equals(""))
    {
        string _RowFilter = keyword+" NOT IN (" + strKeywordInsert + ")";
        dtIns.Clear();
        dtIns = dt2.Copy();
        dtIns.DefaultView.RowFilter = _RowFilter;
        dtIns = dtIns.DefaultView.ToTable();
    }
    if (dt1.Rows.Count == 0)
    {
        dtIns.Clear();
        dtIns = dt2.Copy();
    }

    //<UPDATE>
    dtUpd = dt2.Clone();
    for (int i = 0; i < dt1.Rows.Count; i++)
    {
        for (int j = 0; j < dt2.Rows.Count; j++)
        {
            //假設可變動的keyword與原始的keyword相同
            if (Equals(dt1.Rows[i][keyword], dt2.Rows[j][keyword]))
            {
                for (int c = 0; c < dt1.Columns.Count; c++)
                {
                    if (!Equals(dt1.Rows[i][c], dt2.Rows[j][c]))
                    {
                        dtUpd.ImportRow(dt2.Rows[j]);
                        break;
                    }
                }
            }
        }
    }
}

檢查兩個DataTable是否相同


   1: public static bool AreTablesTheSame(DataTable tbl1, DataTable tbl2)
   2: {
   3:     if (tbl1.Rows.Count != tbl2.Rows.Count || tbl1.Columns.Count != tbl2.Columns.Count)
   4:         return false;
   5:  
   6:  
   7:     for (int i = 0; i < tbl1.Rows.Count; i++)
   8:     {
   9:         for (int c = 0; c < tbl1.Columns.Count; c++)
  10:         {
  11:             if (!Equals(tbl1.Rows[i][c], tbl2.Rows[i][c]))
  12:                 return false;
  13:         }
  14:     }
  15:     return true;
  16: }
原作已忘記是誰

2015年2月8日 星期日

C# 把Form放入TabControl.TabPages

image

image

   1: private void Form1_Load(object sender, EventArgs e)
   2: {
   3:     Form2 f2 = new Form2();
   4:     f2.TopLevel = false;
   5:     f2.Visible = true;
   6:     tabControl1.Dock = DockStyle.Fill;
   7:     tabPage1.Controls.Add(f2);
   8: }

 


image






https://github.com/xxzeng/TabPagesFormDemo/

[C#] 利用NPOI進行EXCEL套表(樞紐分析表)

首先下載NPOI套件
https://npoi.codeplex.com/releases

image

 

image

image

image

image

image

image

   1:  
   2:         private DataTable ProductInfo;
   3:         private void createDataTable() {
   4:  
   5:             ProductInfo = new DataTable();
   6:  
   7:             ProductInfo.Clear();
   8:             ProductInfo.Columns.Add("_Product");
   9:             ProductInfo.Columns.Add("_WH");
  10:             ProductInfo.Columns.Add("_Qty");
  11:  
  12:             DataRow ProductRow = ProductInfo.NewRow();
  13:             ProductRow["_Product"] = "AS133";
  14:             ProductRow["_WH"] = "W101";
  15:             ProductRow["_Qty"] = "10";
  16:             ProductInfo.Rows.Add(ProductRow);
  17:  
  18:             ProductRow = ProductInfo.NewRow();
  19:             ProductRow["_Product"] = "AS133";
  20:             ProductRow["_WH"] = "W102";
  21:             ProductRow["_Qty"] = "7";
  22:             ProductInfo.Rows.Add(ProductRow);
  23:  
  24:             ProductRow = ProductInfo.NewRow();
  25:             ProductRow["_Product"] = "AS133";
  26:             ProductRow["_WH"] = "W103";
  27:             ProductRow["_Qty"] = "5";
  28:             ProductInfo.Rows.Add(ProductRow);
  29:  
  30:             ProductRow = ProductInfo.NewRow();
  31:             ProductRow["_Product"] = "AS156";
  32:             ProductRow["_WH"] = "W101";
  33:             ProductRow["_Qty"] = "6";
  34:             ProductInfo.Rows.Add(ProductRow);
  35:  
  36:             ProductRow = ProductInfo.NewRow();
  37:             ProductRow["_Product"] = "TS156";
  38:             ProductRow["_WH"] = "W101";
  39:             ProductRow["_Qty"] = "8";
  40:             ProductInfo.Rows.Add(ProductRow);
  41:  
  42:             ProductRow = ProductInfo.NewRow();
  43:             ProductRow["_Product"] = "TS156";
  44:             ProductRow["_WH"] = "W102";
  45:             ProductRow["_Qty"] = "8";
  46:             ProductInfo.Rows.Add(ProductRow);
  47:         }


   1:  
   2:         private void button1_Click(object sender, EventArgs e)
   3:         {
   4:             createDataTable();
   5:  
   6:             string strFilePath = string.Format("template.xlt");
   7:             HSSFWorkbook workbook;
   8:             using (FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.ReadWrite))
   9:             {
  10:                 workbook = new HSSFWorkbook(fs);
  11:                 fs.Close();
  12:             }
  13:  
  14:             if (workbook != null)
  15:             {
  16:                 //load template
  17:                 HSSFSheet RawData = (HSSFSheet)workbook.GetSheet("RawData");
  18:  
  19:                 //data to newSheet 
  20:                 HSSFCell hc;
  21:                 HSSFRow hr;
  22:                 HSSFSheet hst = RawData;
  23:  
  24:                 for (int i = 0; i < ProductInfo.Rows.Count; i++)
  25:                 {
  26:                     hr = (HSSFRow)hst.CreateRow(i + 1);
  27:                     for (int j = 0; j < ProductInfo.Columns.Count; j++)
  28:                     {
  29:                         hc = (HSSFCell)hr.CreateCell(j);
  30:                         //Notice!!! Qty is Int,other String.
  31:                         if (ProductInfo.Columns[j].Caption == "_Qty")
  32:                         {
  33:                             hc.SetCellType(CellType.Numeric);
  34:                             if (!string.IsNullOrEmpty(ProductInfo.Rows[i][j].ToString()))
  35:                             {
  36:                                 int number = Convert.ToInt32(ProductInfo.Rows[i][j].ToString());
  37:                                 hc.SetCellValue(number);
  38:                             }
  39:                             else
  40:                             {
  41:                                 hc.SetCellValue(ProductInfo.Rows[i][j].ToString());
  42:                             }
  43:                         }
  44:                         else
  45:                         {
  46:                             hc.SetCellValue(ProductInfo.Rows[i][j].ToString());
  47:                         }
  48:                     }
  49:                 }
  50:  
  51:                 //export new EXCEL file
  52:                 String filename = "P_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".xls";
  53:                 FileStream fsExcelNew = new FileStream(filename, FileMode.Create);
  54:                 workbook.Write(fsExcelNew);
  55:  
  56:                 //remove sheet
  57:                 workbook.RemoveSheetAt(0);
  58:                 workbook = null;
  59:  
  60:                 fsExcelNew.Close();
  61:  
  62:                 //Open excel
  63:                 //System.Windows.Forms.Application.StartupPath + "\\" + filename;
  64:                  string file = @"C:\Windows\explorer.exe";
  65:                  System.Diagnostics.Process.Start(file, filename);
  66:             }
  67:         }

https://github.com/xxzeng/NPOIdemo/

[C#] backgroundworker應用並跳出處理畫面

image

兩個Form

image

image

image

在Form的按鈕寫入下列程式碼

   1: private void button1_Click(object sender, EventArgs e)
   2: {
   3:     processingcs pp = new processingcs();
   4:     if (pp.ShowDialog() == DialogResult.OK) { 
   5:     
   6:     }
   7: }







backgroundWorker1的屬性視窗,點選閃電圖示
然後將三個選項都Double Click.


image


在processingcs寫入下列程式碼



   1:  
   2:         private void processingcs_Load(object sender, EventArgs e)
   3:         {
   4:             backgroundWorker1.RunWorkerAsync();
   5:         }
   6:         private void DoWork() {
   7:             backgroundWorker1.WorkerReportsProgress = true;
   8:             for (int i = progressBar1.Minimum; i <= progressBar1.Maximum; i++)
   9:             {
  10:                 backgroundWorker1.ReportProgress(i);
  11:                 Thread.Sleep(50);
  12:             }
  13:         }
  14:         private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  15:         {
  16:             DoWork();
  17:         }
  18:         private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
  19:         {
  20:             progressBar1.Value = e.ProgressPercentage;
  21:         }
  22:         private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  23:         {
  24:             this.DialogResult = DialogResult.OK;
  25:             this.Close();
  26:         }

範例下載https://github.com/xxzeng/BackgroundworkerDemo.git