2014年7月28日 星期一

調整2維陣列的長度

private static T[,] ResizeArray<T>(T[,] original, int rows, int cols)
        {
            var newArray = new T[rows, cols];
            int minRows = Math.Min(rows, original.GetLength(0));
            int minCols = Math.Min(cols, original.GetLength(1));
            for (int i = 0; i < minRows; i++)
                for (int j = 0; j < minCols; j++)
                    newArray[i, j] = original[i, j];
            return newArray;
        }

2014年7月23日 星期三

ReportViewer使用範例

1.在Form1上面放入ReportViewer物件
2.加入資料集:
專案目錄右鍵 -> 加入 -> 新增項目 -> 資料 -> 資料集 -> 預設名稱 DataSet1.xsd
-> 開啟伺服器總管,點選SQL DB -> 將欲使用的Table拖拉到 DataSet視覺化設計工具
3.點ReportViewer右上角的智慧標籤 -> 設計新報表 -> 資料來源 -> DataSet1 -> 可用資料集
F5執行 -> 確認已可看到資料
4.加入查詢功能:
  4.1  DataSet1.xsd 畫面 -> Fill.GetData() -> 右鍵 -> 設定 -> TableAdapter組態精靈
         -> 加入where條件:where Color=@x -> 下一步 -> 完成
  4.2 Form1,ReportViewer上方放入TextBox1、Button
  4.3 Button 事件,將FormLoad事件內容,載入資料區段放進來,將TableAdapter.Fill
        加入參數 TextBox.Text,ex:this.ProductTableAdapter.Fill(this.DataSet1.Product,textBox1.Text);
        -> F5執行

2014年4月11日 星期五

JQuery Mobile-MVC 動態套用佈景主題

參考P.453~460

1.建立Controller,裡面放2個View:Index、Setting
2.建立Index首頁
3.建立Setting畫面

2014年3月25日 星期二

讀取TXT,內容寫到DataGridView



1.文字檔內容(以逗號將每欄位隔開):
SRV1,192.168.100.1,C:\,100,200
SRV2,192.168.100.2,C:\,100,200
SRV3,192.168.100.3,C:\,100,200

2.程式碼
private void Form1_Load(object sender, EventArgs e)
{
    TextReader txtReader = new StreamReader(@".\DiskInfo.txt");
    while (txtReader.Peek() != -1)
    {
        string[] parts = txtReader.ReadLine().Split(',');
        dataGridView1.Rows.Add(parts);
     }
 txtReader.Close();
}




2014年2月24日 星期一

將檔案或目錄壓縮成ZIP檔;ZIP File or Directory


1.下載 IONIC.ZIP.DLL 後加入到參考
2.using Ionic.Zip;
3.用法:
 private void button1_Click(object sender, EventArgs e)
{
    using (ZipFile zip = new ZipFile(Encoding.Default)) //Encoding.Default 處理中文問題
    {
        string FileName = "file.txt";
       
        //直接加縮
        zip.AddFile(FileName);
     
        //加密碼
        zip.Password = "password";
       
        //存檔
        zip.Save@("C:\")

        //整個目錄壓縮
        string sPathTo = @"C:\Temop";
        zip.AddDirectory(sPathTo);
        zip.Save@("C:\zipfile.zip")
    }
}


2014年1月22日 星期三

在Exchange上,新增、刪除傳輸規則

一、新增規則,若收件人為user1,自動產生副本給user2:
$Condition = Get-TransportRulePredicate SentTo
$Condition.Addresses = (Get-Mailbox "user1")
$Action = Get-TransportRuleAction CopyTo
$Action.Addresses = (Get-Mailbox "user2")
New-TransportRule -Name 'Rule1' -Comments '' -Condition @($Condition) -Actions @($Action) -Enable $true

二、刪除 "Rule1" 的規則
Remove-TransportRule "Rule1" -Confirm:$false

三、自動執行
1.將PowerShell指令以記事本編輯,副檔名ps1存檔
2.(以Windows2008為例)開啟工作排程器 -> 建立工作 -> 將右下角「以最高權限執行」
  ->程式或指令碼欄位輸入「%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe行」
  ->新增引數欄位輸入ps1路徑及檔名「-NoExit -ImportSystemModules c:\newrule.ps1」







2013年12月27日 星期五

Excel相關應用

常常需要透過程式與Excel檔案做一些互動,用久了不外乎就是那幾種應用,這邊做個歸納供需要的時候取用。

一、產生新的Excel檔案
    1.先確定方案總管的參考裡面有Microsoft.Office.Interop.Excel 如果沒有請自行安裝。
    2.using Microsoft.Office.Interop.Excel;
    3.
    //建立Excel物件
    Microsoft.Office.Interop.Excel.Application xlapp =                     
    new Microsoft.Office.Interop.Excel.Application();
    
    //顯式檔案
    xlapp.Visible = true;
    
    //建立WorkBook物件
    Workbook wb = xlapp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            
    //建立WorkSheet物件
    Worksheet ws = (Worksheet)wb.Sheets[1];

    //指定要控制的藍為範圍
    Range rg = ws.get_Range("A:K", System.Type.Missing);
    
    //字型大小
    rg.Font.Size = 10;

    //字型粗體
    rg.Font.Bold = true;

    //儲存格設為純文字
    rg.NumberFormatLocal = "@";

    //背景顏色    
    rg.Cells.Interior.Color = ColorTranslator.ToOle(Color.FromArgb(255,255,0));
    
二、讀取現有的Excel檔案
 
    //宣告路徑字串
    string sFileWithPath = null;

    //宣告開檔對話框的物件  
    OpenFileDialog OFDialog1 = new OpenFileDialog();
 
    //指定預設開己的路徑為桌面
    OFDialog1.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
         
    //預設顯式Excel檔案
    OFDialog1.Filter = "*.xlsx|*.XLSX|*.xls|*.XLS";

    if (OFDialog1.ShowDialog() == DialogResult.OK)
    {
        sFileWithPath = OFDialog1.FileName;
        xlapp.Visible = true;
        Workbook wb = xlapp.Workbooks.Open(sFileWithPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        Worksheet ws = (Worksheet)wb.Sheets[1];     //指定第1個Sheet

        Range rg = ws.get_Range("L:X", System.Type.Missing);    //設定要控制的欄位區間
    .......
    ....
    }

三、金額處理
 若有一金額為99,999,用字串時需要千分位符號,計算時需拿掉。
   String to Double:
      string sMoney = "99,9999";
      double dMoneyCount = Convert.ToDouble(sMoney.Replace(",",""));
  Double to String:
      double dMoneyCount = 999999.0;
      string sMoney = dMoneyCount.ToString("#,##");