用c#把datagridview直接保存成Excel,带样式,支持1997-2010版本

用c#把datagridview直接保存成Excel,带样式,支持1997-2010版本,有需要同学可以用一下

如果需要转载,请注明来源

/// <summary> /// 导出datagirdview的数据到Excel表格,带样式,create by [email protected],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; }

你可能感兴趣的:(C#,Excel,导入,2003,2007,datagridview,2010)