C# 導出Excel表格(WinFrom)


 #region 事件 導出Excel表格 客戶基本信息
        private void btn_Export_Click(object sender, EventArgs e)
        {
            if(this.dgv_CustomerInfo.Rows.Count<1)
            {
                MessageBox.Show("沒有要導出的客戶信息!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;   
            }
            StatesBar sb = new StatesBar();
            try
            {
                saveFileDialog1.Title = "不凡帝訂單系統導出----榮慶物流";
                saveFileDialog1.Filter = "Excel(*.xls)|*.xls";
                saveFileDialog1.FileName = string.Format("客戶基本信息--{0}", DateTime.Now.ToString("yyyyMMdd"));
                DialogResult result = saveFileDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    sb.Show("系統正在處理中...請稍候!", true);
                    Microsoft.Office.Interop.Excel._Application xlapp = new Microsoft.Office.Interop.Excel.Application();
                    Microsoft.Office.Interop.Excel.Workbook xlbook = xlapp.Workbooks.Add(true);
                    Microsoft.Office.Interop.Excel.Worksheet xlsheet = (Microsoft.Office.Interop.Excel.Worksheet)xlbook.Worksheets[1];
                    int colIndex = 0;                 
                    int RowIndex = 1;
                    string headInfo = "客戶名稱,客戶編碼,辦事處,承運商,省份,城市,收貨地址,收貨人,聯系電話,備注,錄入時間";
                    //開始寫入每列的標題
                    foreach (var s in headInfo.Split(','))
                    {
                        colIndex++;
                        xlsheet.Cells[RowIndex, colIndex] = s;
                    }

                    //開始寫入內容
                    int RowCount = this.dgv_CustomerInfo.Rows.Count;//行數
                    for (int i = 0; i < RowCount; i++)
                    {
                        RowIndex++;
                        xlsheet.Cells[RowIndex, 1].NumberFormatLocal = "@";
                        xlsheet.Cells[RowIndex, 1] = dgv_CustomerInfo.Rows[i].Cells["CustomerName"].Value.ToString();
                        xlsheet.Cells[RowIndex, 2].NumberFormatLocal = "@";
                        xlsheet.Cells[RowIndex, 2] = dgv_CustomerInfo.Rows[i].Cells["CustomerCode"].Value.ToString();
                        xlsheet.Cells[RowIndex, 3].NumberFormatLocal = "@";
                        xlsheet.Cells[RowIndex, 3] = dgv_CustomerInfo.Rows[i].Cells["DepartmentName"].Value.ToString();
                        xlsheet.Cells[RowIndex, 4].NumberFormatLocal = "@";
                        xlsheet.Cells[RowIndex, 4] = dgv_CustomerInfo.Rows[i].Cells["CarrierName"].Value.ToString();
                        xlsheet.Cells[RowIndex, 5].NumberFormatLocal = "@";
                        xlsheet.Cells[RowIndex, 5] = dgv_CustomerInfo.Rows[i].Cells["SendProvinceName"].Value.ToString();
                        xlsheet.Cells[RowIndex, 6].NumberFormatLocal = "@";
                        xlsheet.Cells[RowIndex, 6] = dgv_CustomerInfo.Rows[i].Cells["SendCityName"].Value.ToString();
                        xlsheet.Cells[RowIndex, 7].NumberFormatLocal = "@";
                        xlsheet.Cells[RowIndex, 7] = dgv_CustomerInfo.Rows[i].Cells["ReceiveAddress"].Value.ToString();
                        xlsheet.Cells[RowIndex, 8].NumberFormatLocal = "@";
                        xlsheet.Cells[RowIndex, 8] = dgv_CustomerInfo.Rows[i].Cells["ReceiveName"].Value.ToString();
                        xlsheet.Cells[RowIndex, 9].NumberFormatLocal = "@";
                        xlsheet.Cells[RowIndex, 9] = dgv_CustomerInfo.Rows[i].Cells["ReceiceLink"].Value.ToString();
                        xlsheet.Cells[RowIndex, 10].NumberFormatLocal = "@";
                        xlsheet.Cells[RowIndex, 10] = dgv_CustomerInfo.Rows[i].Cells["Remark"].Value.ToString();
                        xlsheet.Cells[RowIndex, 11].NumberFormatLocal = "@";
                        xlsheet.Cells[RowIndex, 11] = dgv_CustomerInfo.Rows[i].Cells["WriteTime"].Value.ToString();
                    }                   
                    xlbook.Saved = true;
                    xlbook.SaveCopyAs(saveFileDialog1.FileName);
                    xlapp.Quit();
                    GC.Collect();

                    #region 強行殺死最近打開的Excel進程
                    System.Diagnostics.Process[] excelProc = System.Diagnostics.Process.GetProcessesByName("EXCEL");
                    System.DateTime startTime = new DateTime();
                    int m, killId = 0;
                    for (m = 0; m < excelProc.Length; m++)
                    {
                        if (startTime < excelProc[m].StartTime)
                        {
                            startTime = excelProc[m].StartTime;
                            killId = m;
                        }
                    }
                    if (excelProc[killId].HasExited == false)
                    {
                        excelProc[killId].Kill();
                    }
                    #endregion

                    sb.Close();
                    MessageBox.Show("導出成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch
            {
                sb.Close();
                MessageBox.Show("導出失敗!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM