回覆列表
  • 1 # 使用者685689750505

    //ProgressBar pb = new ProgressBar();

    object oMissing = System.Reflection.Missing.Value;

    Excel.ApplicationClass xlApp = new Excel.ApplicationClass();

    try

    {

    //開啟EXCEL檔案

    Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filepath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);

    // Excel.Workbook xlWorkbook=xlApp.Workbooks.只有Open屬性,沒有Write屬性

    // Excel.Worksheet xlWorksheet;

    //迴圈所有DataTable

    for (int i = 0; i < ds.Tables.Count; i++)

    {

    //新增入一個新的Sheel頁

    // xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.Add(oMissing, oMissing, 1, oMissing);

    //// 以TableName作為新加的sheel頁名

    // xlWorksheet.Name ="sadasdas";

    Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlApp.ActiveSheet;

    xlWorksheet.Name = SheetName;

    //xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets["ASQ"];

    //xlWorksheet.Activate() ;

    progressBar1.Visible = true;

    progressBar1.Maximum = ds.Tables[i].Rows.Count * ds.Tables[i].Columns.Count;

    progressBar1.Minimum = 0;

    int step = progressBar1.Maximum / ds.Tables[i].Rows.Count;

    progressBar1.Step = step;

    progressBar1.Value = progressBar1.Maximum - step *ds.Tables[i].Rows.Count;

    //取出這個DataTable中的所有值,暫時存於stringBuffer中

    string stringBuffer = "";

    for (int j = 0; j < ds.Tables[i].Rows.Count; j++)

    {

    progressBar1.Value = progressBar1.Value + step;

    for (int k=0; k < ds.Tables[i].Columns.Count; k++)

    {

    cout3 = j; cout4 = k;

    stringBuffer += ds.Tables[i].Rows[j][k].ToString();

    if (k < ds.Tables[i].Columns.Count - 1)

    stringBuffer += "\t";

    }

    stringBuffer += "\n";

    }

    progressBar1.Value = 0;

    //利用系統剪貼簿

    System.Windows.Forms.Clipboard.SetDataObject("");

    //將stringBuffer放入剪貼簿

    System.Windows.Forms.Clipboard.SetDataObject(stringBuffer);

    //選中這個sheel頁中的第一個單元格

    ((Excel.Range)xlWorksheet.Cells[2, 1]).Select();

    //貼上

    xlWorksheet.Paste(oMissing, oMissing);

    //清空系統剪貼簿

    System.Windows.Forms.Clipboard.SetDataObject("");

    }

    //儲存並關閉這個工作薄

    xlWorkbook.Close(Excel.XlSaveAction.xlSaveChanges, oMissing, oMissing);

    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook);

    xlWorkbook = null;

    progressBar1.Visible = false;

    }

    catch (Exception ex)

    {

    MessageBox.Show(ex.Message);

    }

    finally

    {

    //釋放...

    xlApp.Quit();

    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

    xlApp = null;

    GC.Collect();

    // GC.WaitForPendingFinalizers();

    }

  • 中秋節和大豐收的關聯?
  • 三星S21現在3999就能買到,還有必要選小米OPPO嗎?