C# Winform Excel的导出,根据excel模板导出数据

namespace dxhbskymDemo

{

    public partial class ExcelForm : DevExpress.XtraEditors.XtraForm

    {

       public ExcelForm()

        {

            InitializeComponent();



        }

       #region 导出Excel

        

       //导出按钮

        private void sbtnDaochu_Click(object sender, EventArgs e)

        {

            string fileName = "";//要保存的excel文件名

            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter = "导出1997-2003版本Excel(*.xls)|*.xls,导出2007版本Excel(*.xlsx)|*.xlsx";

       //扩展名 sfd.FileName = "河北省项目汇总表";// if (sfd.ShowDialog() == DialogResult.OK) { fileName = sfd.FileName; //导入 outPutExcel(fileName); } }
    //导出Excel的方法

        private void outPutExcel(string saveFilepath)

        {

            bool isShowExcel = false;



            DataTable dt = new DataTable();//将数据库取出的数据源转成DataTable

           

            if (dt == null)

            {

                return;

            }

            if (dt.Rows.Count == 0)

            {

                return;

            }

            Missing miss = Missing.Value;

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

            

            if (xlApp == null)

            {

                XtraMessageBox.Show("请确保您的电脑已经安装Excel!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;

            }

            //xlApp.UserControl = true;

            Excel.Workbooks workBooks = xlApp.Workbooks;

            //Excel.Workbook workBook = workBooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);//创建新的

            Excel.Workbook workBook = workBooks.Add(System.AppDomain.CurrentDomain.BaseDirectory + "excel\\项目汇总表.xls");//根据现有excel模板产生新的Workbook

            Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1];//获取sheet1
 xlApp.DisplayAlerts = false;//保存Excel的时候,不弹出是否保存的窗口直接进行保存
workSheet.get_Range("A3", "B3").Merge(workSheet.get_Range("A3", "B3").MergeCells);//合并单元格 if (workSheet == null) { XtraMessageBox.Show("请确保您的电脑已经安装Excel!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } try { Excel.Range range=null; xlApp.Visible = isShowExcel;//若是true,则在导出的时候会显示excel界面 int totalCount = dt.Rows.Count; workSheet.Cells[1, 1] = "河北省石家庄市项目汇总表";//导出标题 //写入数值 for (int i = 0; i < dt.Rows.Count; i++) { workSheet.Cells[i + 4, 1] = dt.Rows[i]["xmxh"];//项目序号 //workSheet.Cells[5,1]=””;//表示第五行与第一列交点的那个单元格坐标
                    workSheet.Cells[i + 4, 2] = dt.Rows[i]["xmmc"];//项目名称

                 ((Excel.Range)workSheet.Cells[i+4, 2]).ColumnWidth = 12;//设置列宽
                    

                    workSheet.Cells[i + 4, 3] = dt.Rows[i]["bz"];//备注



                    

                }

          //计算sum和
          Excel.Range range2 = workSheet.get_Range("C9", Type.Missing);
          range2.Formula = "=SUM(C4:C8)"; 
          range2.Calculate();



         workBook.SaveAs(saveFilepath,Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                //workSheet.SaveAs(saveFilepath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                workBooks.Close();



            }

            catch (Exception ex)

            {

                XtraMessageBox.Show("Excel导出失败,错误:" + ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

                xlApp.Quit();

                regExpre.KillExcelProcess();

            }

            finally

            {

                xlApp.Quit();

                regExpre.KillExcelProcess();

            }

        }



        #endregion



    }

}

你可能感兴趣的:(WinForm)