废话就不多说了,开始。。。
用c#把datagridview直接保存成Excel,带式样,支撑1997-2010版本,有须要同窗可以用一下
如果须要载转,请注明起源
/// <summary> /// 出导datagirdview的数据到Excel表格,带式样,create by deleteelf@gmail.com,2013-04-25 /// </summary> /// <param name="dgv" />须要出导数据的datagridview /// <param name="worksheetsName" />任务簿称名 /// <param name="saveFileName" />保存的文件名,注意office会检查文件名并正告,为了好友,不要乱输入文件扩展名 /// <returns></returns> protected bool DataGridviewToExcel(DataGridView dgv, string worksheetsName, string saveFileName)//="未命名.xls") { System.Reflection.Missing miss = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.ApplicationClass m_objExcel = new Microsoft.Office.Interop.Excel.ApplicationClass(); m_objExcel.Visible = false; Microsoft.Office.Interop.Excel.XlFileFormat format; switch (Path.GetExtension(saveFileName).ToLower()) { case ".xlsx": if (Convert.ToDouble(m_objExcel.Version) > 12)//2007的版本为13,2010的版本为14 { format = Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault;//2007以上版本的式格 } else { //理清资源 m_objExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel); GC.Collect(); throw new Exception("未安装2007或以上版本的office版本,没法行进保存该式格的excel,请选择低版本的行进保存"); } break; default: format = Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8;//设置认默是97-2003式格 break; } Microsoft.Office.Interop.Excel.Workbook m_objBook = m_objExcel.Workbooks.Add(miss);//创立新的excel Microsoft.Office.Interop.Excel.Worksheet m_objSheet = (Microsoft.Office.Interop.Excel.Worksheet)m_objBook.Worksheets.Add(miss, miss, miss, miss); m_objSheet.Name = worksheetsName;//设置任务簿 Microsoft.Office.Interop.Excel.Range range = null; int rowIndex = 1;//excel索引是从1开始 int columnIndex = 1; for (int i = 0; i < dgv.ColumnCount; i++) { range = m_objSheet.get_Range(m_objSheet.Cells[rowIndex, columnIndex + i], m_objSheet.Cells[rowIndex, columnIndex + i]); range.Interior.Color = dgv.ColumnHeadersDefaultCellStyle.BackColor.ToArgb(); range.HorizontalAlignment = GetExcelAlign(dgv.ColumnHeadersDefaultCellStyle.Alignment); range.Borders.LineStyle = 1; range.ColumnWidth = dgv.Columns[i].Width / 6; m_objSheet.Cells[rowIndex, columnIndex + i] = dgv.Columns[i].HeaderText; } for (int i = 0; i < dgv.RowCount; i++) { for (int j = 0; j < dgv.ColumnCount; j++) { range = m_objSheet.get_Range(m_objSheet.Cells[rowIndex + i + 1, columnIndex + j], m_objSheet.Cells[rowIndex + i + 1, columnIndex + j]); range.Interior.Color = dgv.Rows[i].InheritedStyle.BackColor.ToArgb(); range.HorizontalAlignment = GetExcelAlign(dgv[j, i].InheritedStyle.Alignment); range.Borders.LineStyle = 1; m_objSheet.Cells[rowIndex + i + 1, columnIndex + j] = dgv[j, i].FormattedValue; } } //不行进示提 m_objExcel.DisplayAlerts = false; m_objExcel.AlertBeforeOverwriting = false; try { m_objBook.SaveAs(saveFileName, format, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss); } catch (Exception ex) { throw new Exception("保存时生发错误,文件经已被打开,请先闭关后再保存"); } m_objBook.Close(false, miss, miss); m_objExcel.Workbooks.Close(); m_objExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel); GC.Collect(); return true; }
文章结束给大家分享下程序员的一些笑话语录: 看到有人回帖“不顶不是中国人”,他的本意是想让帖子沉了。