首页 > 代码库 > C#导出Excel动态列

C#导出Excel动态列

一、用StreamWrite流对象,导出Excel

 1、

 string _sPath = GenerateSalaryMonthlyReport(dgvSalarySum); System.Diagnostics.Process.Start(_sPath);

2、

 public String GenerateSalaryMonthlyReport(DataGridView dgvData)        {            String _sPath = String.Format(@".\{0}.txt", BaseClass.getGUID());            String _sGoal = String.Format(@"{0}{1}.xls", System.Configuration.ConfigurationManager.AppSettings["Statistic"],                                                         BaseClass.ExcelFileName("员工每月薪酬表"));            System.IO.StreamWriter _sw = new System.IO.StreamWriter(_sPath, false, Encoding.Default);            //表头            _sw.WriteLine("人力宝人力资源管理系统V2.0");            _sw.WriteLine("薪酬管理模块 <<员工每月薪酬表>>");            _sw.WriteLine();            _sw.Write(String.Format("{0}\t", "序号"));            foreach (DataGridViewColumn dgvc in dgvData.Columns)            {                if (dgvc.Visible)                {                    _sw.Write(String.Format("{0}\t", dgvc.HeaderText));                }            }            _sw.Write(String.Format("{0}\t{1}\t{2}", "员工签字", "日期", "备注"));            _sw.WriteLine();            int i = 1;            foreach (DataGridViewRow dgvr in dgvData.Rows)            {                _sw.Write(String.Format("{0}", i));                for (int j = 0; j < dgvData.Columns.Count - 1; j++)                {                    if (dgvData.Columns[j].Visible)                    {                        if (j == 3)                        {                            _sw.Write(String.Format("\t‘{0}", dgvr.Cells[j].Value));                        }                        else                         {                         _sw.Write(String.Format("\t{0}", dgvr.Cells[j].Value));                        }                    }                }                i++;                _sw.WriteLine();            }            _sw.WriteLine();            _sw.WriteLine(String.Format("{0}\t\t{1}\t\t\t\t{2}\t\t{3}", "制表人", "制表日期", "审核人", "审核日期"));            _sw.Close();            System.IO.File.Move(_sPath, _sGoal);            return _sGoal;        }

 

C#导出Excel动态列