zoukankan      html  css  js  c++  java
  • C#Winform导出Excel

    View Code 
     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.Windows.Forms;
     using Microsoft.Office.Interop.Excel;
     namespace SaleApplication.Class
     {
         class ExportExcelOfWinform
         {
             public static bool OutToExcelFromDataGridView(string title, DataGridView dgv, bool isShowExcel)
             {
                 int titleColumnSpan = 0;//标题的跨列数
                 string fileName = "";//保存的excel文件名
                 int columnIndex = 1;//列索引
                 if (dgv.Rows.Count == 0)
                     return false;
                 /*保存对话框*/
                 SaveFileDialog sfd = new SaveFileDialog();
                 sfd.Filter = "导出Excel(*.xls)|*.xls";
                 sfd.FileName = title + DateTime.Now.ToString("yyyyMMddhhmmss");
                 
                 if (sfd.ShowDialog() == DialogResult.OK)
                 {
                     fileName = sfd.FileName;
                     /*建立Excel对象*/
                     Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                     if (excel == null)
                     {
                         MessageBox.Show("无法创建Excel对象,可能您的计算机未安装Excel!");
                         return false;
                     }
                     try
                     {
                         excel.Application.Workbooks.Add(true);
                         excel.Visible = isShowExcel;
                         /*分析标题的跨列数*/
                         foreach (DataGridViewColumn column in dgv.Columns)
                         {
                             if (column.Visible == true)
                                 titleColumnSpan++;
                         }
                         /*合并标题单元格*/
                         Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.ActiveSheet;
                         //worksheet.get_Range("A1", "C10").Merge();            
                         worksheet.get_Range(worksheet.Cells[11as Range, worksheet.Cells[1, titleColumnSpan] as Range).Merge();
                         /*生成标题*/
                         excel.Cells[11] = title;
                         (excel.Cells[11as Range).HorizontalAlignment = XlHAlign.xlHAlignCenter;//标题居中
     
    //生成字段名称
                         columnIndex = 1;
                         for (int i = 0; i < dgv.ColumnCount; i++)
                         {
                             if (dgv.Columns[i].Visible == true)
                             {
                                 excel.Cells[2, columnIndex] = dgv.Columns[i].HeaderText;
                                 (excel.Cells[2, columnIndex] as Range).HorizontalAlignment = XlHAlign.xlHAlignCenter;//字段居中
                                 columnIndex++;
                             }
                         }
                         //填充数据              
                         for (int i = 0; i < dgv.RowCount; i++)
                         {
                             columnIndex = 1;
                             for (int j = 0; j < dgv.ColumnCount; j++)
                             {
                                 if (dgv.Columns[j].Visible == true)
                                 {
                                     if (dgv[j, i].ValueType == typeof(string))
                                     {
                                         excel.Cells[i + 3, columnIndex] = "'" + dgv[j, i].Value.ToString();
                                     }
                                     else
                                     {
                                         excel.Cells[i + 3, columnIndex] = dgv[j, i].Value.ToString();
                                     }
                                     (excel.Cells[i + 3, columnIndex] as Range).HorizontalAlignment = XlHAlign.xlHAlignLeft;//字段居中
                                     columnIndex++;
                                 }
                             }
                         }
                         worksheet.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);  
                     }
                     catch { }
                     finally
                     {  
                         excel.Quit(); 
                         excel = null;
                         GC.Collect();
                     }
                     //KillProcess("Excel");
                     return true;
                 }
                 else
                 {
                     return false;
                 }
             }
             private static void KillProcess(string processName)//杀死与Excel相关的进程
             {
                 System.Diagnostics.Process myproc = new System.Diagnostics.Process();//得到所有打开的进程
                 try
                 {
                     foreach (System.Diagnostics.Process thisproc in System.Diagnostics.Process.GetProcessesByName(processName))
                     {
                         if (!thisproc.CloseMainWindow())
                         {
                             thisproc.Kill();
                         }
                     }
                 }
                 catch (Exception Exc)
                 {
                     throw new Exception("", Exc);
                 }
             }
     
         }
     }
  • 相关阅读:
    HTML笔记
    Android自定义View 自定义组合控件
    CSS 笔记
    HTML 4.01 快速参考
    MSP430单片机之中断服务
    MSP430单片机之RTC实时时钟
    Centos7.4内核符号地址查找函数的BUG
    珍惜世上的五个人
    实习
    毕业后的五年拉开大家差距的原因在哪里
  • 原文地址:https://www.cnblogs.com/sydeveloper/p/2406558.html
Copyright © 2011-2022 走看看