首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 匯出資料到 Excel 綜合方案(四):WinForms 環境下使用 Excel COM 組件完整實作

     編輯:關於C語言

方法二:Excel COM 自動化(WinForms 適用)

protected void ExportExcel() {
    gridbind();
    if (ds1 == null) return;

    SaveFileDialog saveDialog = new SaveFileDialog();
    saveDialog.DefaultExt = "xls";
    saveDialog.Filter = "Excel檔案|*.xls";
    saveDialog.FileName = "Sheet1";
    saveDialog.ShowDialog();
    string saveFileName = saveDialog.FileName;
    if (saveFileName.IndexOf(":") < 0) return; // 使用者取消

    Excel.Application xlApp = new Excel.Application();
    object missing = System.Reflection.Missing.Value;
    if (xlApp == null) {
        MessageBox.Show("無法建立 Excel 物件,請確認已安裝 Excel");
        return;
    }

    Excel.Workbooks workbooks = xlApp.Workbooks;
    Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
    Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
    Excel.Range range;

    string oldCaption = Title_label.Text.Trim();
    long totalCount = ds1.Tables[0].Rows.Count;
    long rowRead = 0;
    float percent = 0;

    worksheet.Cells[1, 1] = Title_label.Text.Trim();

    // 寫入欄位標題
    for (int i = 0; i < ds1.Tables[0].Columns.Count; i++) {
        worksheet.Cells[2, i + 1] = ds1.Tables[0].Columns[i].ColumnName;
        range = (Excel.Range)worksheet.Cells[2, i + 1];
        range.Interior.ColorIndex = 15;
        range.Font.Bold = true;
    }

    // 寫入資料列
    Caption.Visible = true;
    for (int r = 0; r < ds1.Tables[0].Rows.Count; r++) {
        for (int i = 0; i < ds1.Tables[0].Columns.Count; i++) {
            worksheet.Cells[r + 3, i + 1] = ds1.Tables[0].Rows[r][i];
        }
        rowRead++;
        percent = (float)(100 * rowRead) / totalCount;
        this.Caption.Text = "正在匯出資料[" + percent.ToString("0.00") + "%]...";
        Application.DoEvents();
    }

    // 套用框線樣式
    range = worksheet.get_Range(worksheet.Cells[2, 1],
                 worksheet.Cells[ds1.Tables[0].Rows.Count + 2, ds1.Tables[0].Columns.Count]);
    range.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin,
                       Excel.XlColorIndex.xlColorIndexAutomatic, missing);
    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous;
    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
    if (ds1.Tables[0].Columns.Count > 1) {
        range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
    }

    worksheet.SaveAs(saveFileName, missing, missing, missing, missing, missing, missing, missing, missing);
    this.Caption.Visible = false;
    this.Caption.Text = oldCaption;
    workbook.Close(missing, missing, missing);
    xlApp.Quit();
}

補充說明:ASP.NET 與 WinForms 差異

  • ASP.NET:伺服器端讀取資料,以 Response 輸出 application/ms-excel 內容至瀏覽器(客戶端)。
  • WinForms:資料已在客戶端,透過呼叫客戶端安裝的 Office COM 組件寫入活頁簿。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved