1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > C#中将DataGridView中的数据导入到Csv文件及导出到Excel

C#中将DataGridView中的数据导入到Csv文件及导出到Excel

时间:2021-01-12 04:12:35

相关推荐

C#中将DataGridView中的数据导入到Csv文件及导出到Excel

后端开发|C#.Net教程

C#,DataGridView

后端开发-C#.Net教程

1,将DataGridView中的数据导入到Csv文件中

微任务发布平台源码,vscode跳转块,电脑ubuntu命令,tomcat放入图片,qt配置sqlite,网络 服务器 选择,论坛自动回复插件,前端框架配置教程外包,爬虫脚本格式,学php零基础,网站seo关键词排名优化,二手书店网站源代码,网页设计hover标签,layer模板,授权页面模板,订票管理系统界面源码,微信公众号系统程序lzw

public static bool dataGridViewToCSV(DataGridView dataGridView) { if (dataGridView.Rows.Count == 0) {MessageBox.Show("没有数据可导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);return false; } SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "CSV files (*.csv)|*.csv"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; saveFileDialog.FileName = null; saveFileDialog.Title = "保存"; if (saveFileDialog.ShowDialog() == DialogResult.OK) {Stream stream = saveFileDialog.OpenFile();StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.GetEncoding(-0));string strLine = "";try{ //表头 for (int i = 0; i 0) strLine += ","; strLine += dataGridView.Columns[i].HeaderText; } strLine.Remove(strLine.Length - 1); sw.WriteLine(strLine); strLine = ""; //表的内容 for (int j = 0; j < dataGridView.Rows.Count; j++) { strLine = ""; int colCount = dataGridView.Columns.Count; for (int k = 0; k 0 && k < colCount)strLine += ","; if (dataGridView.Rows[j].Cells[k].Value == null)strLine += ""; else {string cell = dataGridView.Rows[j].Cells[k].Value.ToString().Trim();//防止里面含有特殊符号cell = cell.Replace("\"", "\"\"");cell = "\"" + cell + "\"";strLine += cell; } } sw.WriteLine(strLine); } sw.Close(); stream.Close(); MessageBox.Show("数据被导出到:" + saveFileDialog.FileName.ToString(), "导出完毕", MessageBoxButtons.OK, MessageBoxIcon.Information);}catch (Exception ex){ MessageBox.Show(ex.Message, "导出错误", MessageBoxButtons.OK, MessageBoxIcon.Information); return false;} } return true; }

2,C#中将DataGridView中的数据导出到Excel中

无版权源码,ubuntu搭建翼龙,蕾姆爬虫图片,php 订单拆分,网站seo首推lzw

public static bool dataGridViewToExcel(DataGridView dataGridView) { if (dataGridView.Rows.Count == 0) {MessageBox.Show("没有数据可导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);return false; } string fileName = ""; string saveFileName = ""; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = "xlsx"; saveDialog.Filter = "Excel文件|*.xlsx"; saveDialog.FileName = fileName; saveDialog.ShowDialog(); saveFileName = saveDialog.FileName; if (saveFileName.IndexOf(":") < 0)return false; //被点了取消 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) {MessageBox.Show("无法创建Excel对象,您的电脑可能未安装Excel");return false; } Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks; Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1//写入标题for (int i = 0; i < dataGridView.ColumnCount; i++) { worksheet.Cells[1, i + 1] = dataGridView.Columns[i].HeaderText; } //写入数值 for (int r = 0; r < dataGridView.Rows.Count; r++) {for (int i = 0; i < dataGridView.ColumnCount; i++){ worksheet.Cells[r + 2, i + 1] = dataGridView.Rows[r].Cells[i].Value;}System.Windows.Forms.Application.DoEvents(); } worksheet.Columns.EntireColumn.AutoFit();//列宽自适应 MessageBox.Show(fileName + "资料保存成功", "提示", MessageBoxButtons.OK); if (saveFileName != "") {try{ workbook.Saved = true; workbook.SaveCopyAs(saveFileName); //fileSaved = true; }catch (Exception ex){//fileSaved = false;MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);} } xlApp.Quit(); Kill(xlApp); GC.Collect();//强行销毁 return true; }

//*********************************************************************************************// //*功能:当使用Excel.Application方法的时候,系统会自动创建一个Excel进程,即使当你使用Excel.Quit()之后,也是不会关闭的。 //*参数:通过进程ID来唯一标识我们自己创建的进程,然后在关闭Excel.Application的时候一同将进程Kill掉。 //*返回值:无 //*时间:-04-25 //**********************************************************************************************// public static void Kill(Microsoft.Office.Interop.Excel.Application excel) { IntPtr t = new IntPtr(excel.Hwnd); int k = 0; GetWindowThreadProcessId(t, out k); System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k); p.Kill(); }

整站脱拖源码,vscode 怎么配置qt,ubuntu进gnome,tomcat运行报异常,sqlite3数据库修复,全网爬虫是通用爬虫吗,mac php编译安装,佛山短视频seo优化,网站图片弹窗代码大全,网页模板小偷18lzw

C#创建Excel文件并将数据导出到Excel文件的示例代码详解

c# 将Datatable数据导出到Excel表格中的示例代码分享

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。