惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - 夜来风雨香

【整理】C#文件操作大全(SamWang)<转> c# 文件及目录操作类 BIOS设置之UEFI/Legacy BIOS切换图文详解 UEFI+GPT引导实践篇(一):切换到UEFI启动,准备安装介质 UEFI+GPT引导实践篇(二):UEFI引导安装64位Win7/Win8 UEFI+GPT引导基础篇(一):什么是GPT,什么是UEFI? 预装WIN8系统的电脑安装WIN7的方法 串口 COM口 TTL RS-232 RS-485 区别 释疑 WinForm DataGridView分页功能 C#GridViewExport帮助类,美化导出 C#EXCEL 操作类--C#DataToExcel帮助类 C#EXCEL 操作类--C#ExcelHelper操作类 VS2010 项目引用了DLL文件,也写了Using,但是编译时提示:未能找到类型或命名空间名称 <转> FPGA Verilog HDL 系列实例--------步进电机驱动控制 C++使用VARIANT实现二维数组的操作 VS2010+VMWare8+VisualDDK1.5.6 创建并调试你的第一个驱动程序 - 完全教程 USB编程研究之二(常见设备类型的GUID) C++——CString用法大全 C++ 如何有效地使用对话框
DataGridView导出到Excel的三个方法
夜来风雨香 · 2014-08-15 · via 博客园 - 夜来风雨香
  1. #region DataGridView数据显示到Excel     
  2. public bool DataGridviewShowToExcel(DataGridView dgv, bool isShowExcle)     
  3. {     
  4.     if (dgv.Rows.Count == 0)     
  5.         return false;     
  6.     
  7.     Excel.Application excel = new Excel.Application();     
  8.     excel.Application.Workbooks.Add(true);     
  9.     excel.Visible = isShowExcle;     
  10.     
  11.     for (int i = 0; i < dgv.ColumnCount; i++)     
  12.     {     
  13.         excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;     
  14.     }     
  15.     
  16.     for (int i = 0; i < dgv.RowCount - 1; i++)     
  17.     {     
  18.         for (int j = 0; j < dgv.ColumnCount; j++)     
  19.         {     
  20.             if (dgv[j, i].ValueType == typeof(string))     
  21.             {     
  22.                 excel.Cells[i + 2, j + 1] = "'" + dgv[j, i].Value.ToString();     
  23.             }     
  24.             else    
  25.             {     
  26.                 excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString();     
  27.             }     
  28.         }     
  29.     }     
  30.     return true;     
  31. }    
  32. #endregion     
  33.   
  34. #region DateGridView导出到csv格式的Excel     
  35. private void DataGridViewToExcel(DataGridView dgv)     
  36. {     
  37.     SaveFileDialog dlg = new SaveFileDialog();     
  38.     dlg.Filter = "Execl files (*.xls)|*.xls";     
  39.     dlg.FilterIndex = 0;     
  40.     dlg.RestoreDirectory = true;     
  41.     dlg.CreatePrompt = true;     
  42.     dlg.Title = "保存为Excel文件";     
  43.     
  44.     if (dlg.ShowDialog() == DialogResult.OK)     
  45.     {     
  46.         Stream myStream;     
  47.         myStream = dlg.OpenFile();     
  48.         StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));     
  49.         string columnTitle = "";     
  50.         try    
  51.         {     
  52.             
  53.             for (int i = 0; i < dgv.ColumnCount; i++)     
  54.             {     
  55.                 if (i > 0)     
  56.                 {     
  57.                     columnTitle += "/t";     
  58.                 }     
  59.                 columnTitle += dgv.Columns[i].HeaderText;     
  60.             }     
  61.             sw.WriteLine(columnTitle);     
  62.     
  63.             
  64.             for (int j = 0; j < dgv.Rows.Count; j++)     
  65.             {     
  66.                 string columnValue = "";     
  67.                 for (int k = 0; k < dgv.Columns.Count; k++)     
  68.                 {     
  69.                     if (k > 0)     
  70.                     {     
  71.                         columnValue += "/t";     
  72.                     }     
  73.                     if (dgv.Rows[j].Cells[k].Value == null)     
  74.                         columnValue += "";     
  75.                     else    
  76.                         columnValue += dgv.Rows[j].Cells[k].Value.ToString().Trim();     
  77.                 }     
  78.                 sw.WriteLine(columnValue);     
  79.             }     
  80.             sw.Close();     
  81.             myStream.Close();     
  82.         }     
  83.         catch (Exception e)     
  84.         {     
  85.             MessageBox.Show(e.ToString());     
  86.         }     
  87.         finally    
  88.         {     
  89.             sw.Close();     
  90.             myStream.Close();     
  91.         }     
  92.     }     
  93. }     
  94. #endregion    
  95.   
  96. #region DataGridView导出到Excel,有一定的判断性     
  97. public static void DataGridViewToExcel(DataGridView dgv)     
  98. {    
  99.   
  100.   
  101.     #region   验证可操作性     
  102.     
  103.     
  104.     SaveFileDialog dlg = new SaveFileDialog();     
  105.     
  106.     dlg.DefaultExt = "xls ";     
  107.     
  108.     dlg.Filter = "EXCEL文件(*.XLS)|*.xls ";     
  109.     
  110.     dlg.InitialDirectory = Directory.GetCurrentDirectory();     
  111.     
  112.     if (dlg.ShowDialog() == DialogResult.Cancel) return;     
  113.     
  114.     string fileNameString = dlg.FileName;     
  115.     
  116.     if (fileNameString.Trim() == " ")     
  117.     { return; }     
  118.     
  119.     int rowscount = dgv.Rows.Count;     
  120.     int colscount = dgv.Columns.Count;     
  121.     
  122.     if (rowscount <= 0)     
  123.     {     
  124.         MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);     
  125.         return;     
  126.     }     
  127.     
  128.     
  129.     if (colscount <= 0)     
  130.     {     
  131.         MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);     
  132.         return;     
  133.     }     
  134.     
  135.     
  136.     if (rowscount > 65536)     
  137.     {     
  138.         MessageBox.Show("数据记录数太多(最多不能超过65536条),不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);     
  139.         return;     
  140.     }     
  141.     
  142.     
  143.     if (colscount > 255)     
  144.     {     
  145.         MessageBox.Show("数据记录行数太多,不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);     
  146.         return;     
  147.     }     
  148.     
  149.     
  150.     FileInfo file = new FileInfo(fileNameString);     
  151.     if (file.Exists)     
  152.     {     
  153.         try    
  154.         {     
  155.             file.Delete();     
  156.         }     
  157.         catch (Exception error)     
  158.         {     
  159.             MessageBox.Show(error.Message, "删除失败 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);     
  160.             return;     
  161.         }     
  162.     }    
  163.     #endregion     
  164.     Excel.Application objExcel = null;     
  165.     Excel.Workbook objWorkbook = null;     
  166.     Excel.Worksheet objsheet = null;     
  167.     try    
  168.     {     
  169.         
  170.         objExcel = new Microsoft.Office.Interop.Excel.Application();     
  171.         objWorkbook = objExcel.Workbooks.Add(Missing.Value);     
  172.         objsheet = (Excel.Worksheet)objWorkbook.ActiveSheet;     
  173.         
  174.         objExcel.Visible = false;     
  175.     
  176.         
  177.         int displayColumnsCount = 1;     
  178.         for (int i = 0; i <= dgv.ColumnCount - 1; i++)     
  179.         {     
  180.             if (dgv.Columns[i].Visible == true)     
  181.             {     
  182.                 objExcel.Cells[1, displayColumnsCount] = dgv.Columns[i].HeaderText.Trim();     
  183.                 displayColumnsCount++;     
  184.             }     
  185.         }     
  186.         
  187.         
  188.         
  189.         
  190.         
  191.         
  192.         
  193.         for (int row = 0; row <= dgv.RowCount - 1; row++)     
  194.         {     
  195.             
  196.     
  197.             displayColumnsCount = 1;     
  198.             for (int col = 0; col < colscount; col++)     
  199.             {     
  200.                 if (dgv.Columns[col].Visible == true)     
  201.                 {     
  202.                     try    
  203.                     {     
  204.                         objExcel.Cells[row + 2, displayColumnsCount] = dgv.Rows[row].Cells[col].Value.ToString().Trim();     
  205.                         displayColumnsCount++;     
  206.                     }     
  207.                     catch (Exception)     
  208.                     {     
  209.     
  210.                     }     
  211.     
  212.                 }     
  213.             }     
  214.         }     
  215.         
  216.         
  217.         
  218.         objWorkbook.SaveAs(fileNameString, Missing.Value, Missing.Value, Missing.Value, Missing.Value,     
  219.                 Missing.Value, Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value,     
  220.                 Missing.Value, Missing.Value);     
  221.     }     
  222.     catch (Exception error)     
  223.     {     
  224.         MessageBox.Show(error.Message, "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);     
  225.         return;     
  226.     }     
  227.     finally    
  228.     {     
  229.         
  230.         if (objWorkbook != null) objWorkbook.Close(Missing.Value, Missing.Value, Missing.Value);     
  231.         if (objExcel.Workbooks != null) objExcel.Workbooks.Close();     
  232.         if (objExcel != null) objExcel.Quit();     
  233.     
  234.         objsheet = null;     
  235.         objWorkbook = null;     
  236.         objExcel = null;     
  237.     }     
  238.     MessageBox.Show(fileNameString + "/n/n导出完毕! ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);     
  239.     
  240. }    
  241.   
  242. #endregion    
  243.  
  244. #region DataGridView数据显示到Excel  
  245. public bool DataGridviewShowToExcel(DataGridView dgv, bool isShowExcle)  
  246. {  
  247.     if (dgv.Rows.Count == 0)  
  248.         return false;  
  249.     
  250.     Excel.Application excel = new Excel.Application();  
  251.     excel.Application.Workbooks.Add(true);  
  252.     excel.Visible = isShowExcle;  
  253.     
  254.     for (int i = 0; i < dgv.ColumnCount; i++)  
  255.     {  
  256.         excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;  
  257.     }  
  258.     
  259.     for (int i = 0; i < dgv.RowCount - 1; i++)  
  260.     {  
  261.         for (int j = 0; j < dgv.ColumnCount; j++)  
  262.         {  
  263.             if (dgv[j, i].ValueType == typeof(string))  
  264.             {  
  265.                 excel.Cells[i + 2, j + 1] = "'" + dgv[j, i].Value.ToString();  
  266.             }  
  267.             else  
  268.             {  
  269.                 excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString();  
  270.             }  
  271.         }  
  272.     }  
  273.     return true;  
  274. }  
  275. #endregion   
  276.  
  277. #region DateGridView导出到csv格式的Excel  
  278. private void DataGridViewToExcel(DataGridView dgv)  
  279. {  
  280.     SaveFileDialog dlg = new SaveFileDialog();  
  281.     dlg.Filter = "Execl files (*.xls)|*.xls";  
  282.     dlg.FilterIndex = 0;  
  283.     dlg.RestoreDirectory = true;  
  284.     dlg.CreatePrompt = true;  
  285.     dlg.Title = "保存为Excel文件";  
  286.   
  287.     if (dlg.ShowDialog() == DialogResult.OK)  
  288.     {  
  289.         Stream myStream;  
  290.         myStream = dlg.OpenFile();  
  291.         StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));  
  292.         string columnTitle = "";  
  293.         try  
  294.         {  
  295.             
  296.             for (int i = 0; i < dgv.ColumnCount; i++)  
  297.             {  
  298.                 if (i > 0)  
  299.                 {  
  300.                     columnTitle += "/t";  
  301.                 }  
  302.                 columnTitle += dgv.Columns[i].HeaderText;  
  303.             }  
  304.             sw.WriteLine(columnTitle);  
  305.   
  306.             
  307.             for (int j = 0; j < dgv.Rows.Count; j++)  
  308.             {  
  309.                 string columnValue = "";  
  310.                 for (int k = 0; k < dgv.Columns.Count; k++)  
  311.                 {  
  312.                     if (k > 0)  
  313.                     {  
  314.                         columnValue += "/t";  
  315.                     }  
  316.                     if (dgv.Rows[j].Cells[k].Value == null)  
  317.                         columnValue += "";  
  318.                     else  
  319.                         columnValue += dgv.Rows[j].Cells[k].Value.ToString().Trim();  
  320.                 }  
  321.                 sw.WriteLine(columnValue);  
  322.             }  
  323.             sw.Close();  
  324.             myStream.Close();  
  325.         }  
  326.         catch (Exception e)  
  327.         {  
  328.             MessageBox.Show(e.ToString());  
  329.         }  
  330.         finally  
  331.         {  
  332.             sw.Close();  
  333.             myStream.Close();  
  334.         }  
  335.     }  
  336. }   
  337. #endregion  
  338.  
  339. #region DataGridView导出到Excel,有一定的判断性  
  340. public static void DataGridViewToExcel(DataGridView dgv)  
  341. {  
  342.  
  343.  
  344.     #region   验证可操作性  
  345.   
  346.     
  347.     SaveFileDialog dlg = new SaveFileDialog();  
  348.     
  349.     dlg.DefaultExt = "xls ";  
  350.     
  351.     dlg.Filter = "EXCEL文件(*.XLS)|*.xls ";  
  352.     
  353.     dlg.InitialDirectory = Directory.GetCurrentDirectory();  
  354.     
  355.     if (dlg.ShowDialog() == DialogResult.Cancel) return;  
  356.     
  357.     string fileNameString = dlg.FileName;  
  358.     
  359.     if (fileNameString.Trim() == " ")  
  360.     { return; }  
  361.     
  362.     int rowscount = dgv.Rows.Count;  
  363.     int colscount = dgv.Columns.Count;  
  364.     
  365.     if (rowscount <= 0)  
  366.     {  
  367.         MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  368.         return;  
  369.     }  
  370.   
  371.     
  372.     if (colscount <= 0)  
  373.     {  
  374.         MessageBox.Show("没有数据可供保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  375.         return;  
  376.     }  
  377.   
  378.     
  379.     if (rowscount > 65536)  
  380.     {  
  381.         MessageBox.Show("数据记录数太多(最多不能超过65536条),不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  382.         return;  
  383.     }  
  384.   
  385.     
  386.     if (colscount > 255)  
  387.     {  
  388.         MessageBox.Show("数据记录行数太多,不能保存 ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  389.         return;  
  390.     }  
  391.   
  392.     
  393.     FileInfo file = new FileInfo(fileNameString);  
  394.     if (file.Exists)  
  395.     {  
  396.         try  
  397.         {  
  398.             file.Delete();  
  399.         }  
  400.         catch (Exception error)  
  401.         {  
  402.             MessageBox.Show(error.Message, "删除失败 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);  
  403.             return;  
  404.         }  
  405.     }  
  406.     #endregion  
  407.     Excel.Application objExcel = null;  
  408.     Excel.Workbook objWorkbook = null;  
  409.     Excel.Worksheet objsheet = null;  
  410.     try  
  411.     {  
  412.         
  413.         objExcel = new Microsoft.Office.Interop.Excel.Application();  
  414.         objWorkbook = objExcel.Workbooks.Add(Missing.Value);  
  415.         objsheet = (Excel.Worksheet)objWorkbook.ActiveSheet;  
  416.         
  417.         objExcel.Visible = false;  
  418.   
  419.         
  420.         int displayColumnsCount = 1;  
  421.         for (int i = 0; i <= dgv.ColumnCount - 1; i++)  
  422.         {  
  423.             if (dgv.Columns[i].Visible == true)  
  424.             {  
  425.                 objExcel.Cells[1, displayColumnsCount] = dgv.Columns[i].HeaderText.Trim();  
  426.                 displayColumnsCount++;  
  427.             }  
  428.         }  
  429.         
  430.         
  431.         
  432.         
  433.         
  434.         
  435.         
  436.         for (int row = 0; row <= dgv.RowCount - 1; row++)  
  437.         {  
  438.             
  439.   
  440.             displayColumnsCount = 1;  
  441.             for (int col = 0; col < colscount; col++)  
  442.             {  
  443.                 if (dgv.Columns[col].Visible == true)  
  444.                 {  
  445.                     try  
  446.                     {  
  447.                         objExcel.Cells[row + 2, displayColumnsCount] = dgv.Rows[row].Cells[col].Value.ToString().Trim();  
  448.                         displayColumnsCount++;  
  449.                     }  
  450.                     catch (Exception)  
  451.                     {  
  452.   
  453.                     }  
  454.   
  455.                 }  
  456.             }  
  457.         }  
  458.         
  459.         
  460.         
  461.         objWorkbook.SaveAs(fileNameString, Missing.Value, Missing.Value, Missing.Value, Missing.Value,  
  462.                 Missing.Value, Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value,  
  463.                 Missing.Value, Missing.Value);  
  464.     }  
  465.     catch (Exception error)  
  466.     {  
  467.         MessageBox.Show(error.Message, "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);  
  468.         return;  
  469.     }  
  470.     finally  
  471.     {  
  472.         
  473.         if (objWorkbook != null) objWorkbook.Close(Missing.Value, Missing.Value, Missing.Value);  
  474.         if (objExcel.Workbooks != null) objExcel.Workbooks.Close();  
  475.         if (objExcel != null) objExcel.Quit();  
  476.   
  477.         objsheet = null;  
  478.         objWorkbook = null;  
  479.         objExcel = null;  
  480.     }  
  481.     MessageBox.Show(fileNameString + "/n/n导出完毕! ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  482.   
  483. }  
  484.  
  485. #endregion