/// <summary> /// DataGridView添加行号 /// </summary> /// <param name="dgv">DataGridView控件ID</param> public static void AddRowIndex(this DataGridView dgv) { dgv.RowPostPaint += delegate(object sender, DataGridViewRowPostPaintEventArgs e) { SolidBrush b = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor); e.Graphics.DrawString((e.RowIndex + 1).ToString(), dgv.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4); }; } /// <summary> /// DataGridView添加全选 /// </summary> /// <param name="dgv">DataGridView控件ID</param> /// <param name="columnIndex">全选所在列序号</param> public static void AddFullSelect(this DataGridView dgv, int columnIndex) { if (dgv.Rows.Count < 1) { return; } CheckBox ckBox = new CheckBox(); Rectangle rect = dgv.GetCellDisplayRectangle(1, -1, true); ckBox.Size = new Size(dgv.Columns[1].Width - 12, 12); //大小 Point point = new Point(rect.X + 10, rect.Y + 3);//位置 ckBox.Location = point; ckBox.CheckedChanged += delegate(object sender, EventArgs e) { for (int i = 0; i < dgv.Rows.Count; i++) { dgv.Rows[i].Cells[columnIndex].Value = ((CheckBox)sender).Checked; } dgv.EndEdit(); }; dgv.Controls.Add(ckBox); } /// <summary> /// 导出到Excel(通过引用excel组件导出) /// </summary> /// <param name="dgv">DataGridView控件ID</param> public static void ExportExcel(this DataGridView dgv) { if (dgv.Rows.Count == 0) return; //实例化一个Excel.Application对象 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); try { //让后台执行设置为不可见,为true的话会看到打开一个Excel,然后数据在往里写 excel.Visible = false; //设置禁止弹出保存和覆盖的询问提示框 excel.DisplayAlerts = false; excel.AlertBeforeOverwriting = false; //新增加一个工作簿,Workbook是直接保存,不会弹出保存对话框,加上Application会弹出保存对话框,值为false会报错 excel.Application.Workbooks.Add(true); //生成Excel中列头名称 for (int i = 0; i < dgv.Columns.Count; i++) { excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText; } //把DataGridView当前页的数据保存在Excel中 for (int i = 0; i < dgv.Rows.Count; i++) { for (int j = 0; j < dgv.Columns.Count; j++) { if (dgv[j, i].ValueType == typeof(string)) { excel.Cells[i + 2, j + 1] = "'" + dgv[j, i].Value.ToString(); } else { excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString(); } } } //保存excel文件 excel.Save(System.AppDomain.CurrentDomain.BaseDirectory + "temp.xls"); } catch { } finally { excel.Quit(); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excel); excel = null; GC.Collect(); } } /// <summary> /// 导出到Excel(通过生成html导出) /// </summary> /// <param name="dgv">DataGridView控件ID</param> public static void ExportExcel(this DataGridView dgv) { if (dgv.Rows.Count == 0) return; const string HEADER = "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">" + "<meta http-equiv=Content-Type content=\"text/html; charset=\"gb2312\">" + "<head>" + "<!--[if gte mso 9]><xml>" + "<x:ExcelWorkbook>" + "<x:ExcelWorksheets>" + "<x:ExcelWorksheet>" + "<x:Name>工作表标题</x:Name>" + "<x:WorksheetOptions>" + "<x:Print>" + "<x:ValidPrinterInfo />" + "</x:Print>" + "</x:WorksheetOptions>" + "</x:ExcelWorksheet>" + "</x:ExcelWorksheets>" + "</x:ExcelWorkbook>" + "</xml>" + "<![endif]-->"; const string STYLE = "<style type=\"text/css\">" + ".text" + " {" + " vnd.ms-excel.numberformat:@;" + " }" + "</style>"; using(SaveFileDialog saveFileDialog = new SaveFileDialog()) { saveFileDialog.Title = "导出Excel文件到"; saveFileDialog.Filter = "Execl files(*.xls)|All Files(*.*)"; saveFileDialog.FileName = DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".xls"; saveFileDialog.AddExtension = true; saveFileDialog.RestoreDirectory = true; if (saveFileDialog.ShowDialog() == DialogResult.OK) { using (StreamWriter writer = new StreamWriter(saveFileDialog.FileName, false, System.Text.Encoding.GetEncoding("gb2312"), 1024)) { writer.WriteLine(HEADER); writer.WriteLine(STYLE); writer.WriteLine("</head><body><table border=\"1\" style=\"font-size:9pt\"><tr>"); for (int i = 0; i < dgv.Columns.Count; i++) { writer.WriteLine(string.Format("<th>{0}</th>", dgv.Columns[i].HeaderText)); } for (int i = 0; i < dgv.Rows.Count; i++) { writer.WriteLine("<tr>"); for (int j = 0; j < dgv.Columns.Count; j++) { writer.WriteLine(string.Format("<td class=\"text\">{0}</td>", dgv[j, i].Value.ToString())); } writer.WriteLine("</tr>"); } writer.WriteLine("</table></body>"); } } } }
详细解决方案
DataGridView扩张方法行号、全选、导出到Excel(引用excel组件、生成html两种方式)
热度:178 发布时间:2013-01-17 10:28:54.0
相关解决方案
- 访问Tomcat的url的时候如何自动调用index.html
- 导出 Excel 资料怎样命名
- The requested resource (/webtest/servlet/hello.html) is not availabl 帮忙解决解决方法
- JSP 页面乱码 页面起首已设置 contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
- word excel pdf在ie浏览器中打开,该如何解决
- 在浏览器中展示word,excel.ppt,pdf等各种文件
- POI 处置 Excel,读取Excel中的格式 如表格框,背景色
- jxls 怎么导出图片到 excel 中
- html js不运行有关问题
- 办公自动化系统——议程管理(用jsp+servlet+js+html+jdbc)怎样实现
- <html:text>property的有关问题
- 关于<html:text>相关的有关问题
- poi excel 如何合并单元格?
- Struts 1.2 html:form的action和form的action区别,还有如果2个都有执行哪个解决方案
- 简单计算器(html+js),该怎么解决
- HTML 小疑点,哪位高手解决 给哪位高手分
- 嵌入JSP中的CSS显示效果和在 HTML 中的不一样.为什么.该怎么解决
- struts1 中<html:checkbox>标签的value值怎么动态赋值
- 新人求问,J2EE方向,html,css,javascript,vml要学到什么程度?解决思路
- :前台和后台开发有啥区别?还有css、html、ajax、js、jquery都有什么区别
- 用Java EXCEL API 是否能从Excel中导出表格,该如何处理
- java excel 問題 求大神 速解解决方案
- struts <html:file> 怎么让前面的框消失,先谢过了
- java 处理 excel 相关有关问题
- 求Microsoft.Office.Interop.Excel.dll 11.0.0.0解决方法
- DataGridView:为何RowTemplate.Height无法调整第0行的行高
- Microsoft.Office.Interop.Excel.dll 导出的文件版本的有关问题
- =Html.TextAreaFor的文本区域大小如何设置
- WinFrom 中 DataGridView 的 DataGridViewComboBoxColumn 列 如何 设置默认项
- VC 2010 怎么 操作 excel 了