首页 > 代码库 > 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动态列
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。