[WinForm]dataGridView导出到EXCEL
方法一:
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Execl files (*.xls)|*.xls";
dlg.FilterIndex = 0;
dlg.RestoreDirectory = true;
dlg.CreatePrompt = true;
dlg.Title = "保存为Excel文件";
dlg.FileName = "不合格记录";//保存的Excel名字
if (dlg.ShowDialog() == DialogResult.OK)
{
Stream myStream;
myStream = dlg.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
string columnTitle = "";
try
{
//写入列标题
for (int i = 0; i < dgv.ColumnCount; i++)
{
if (i > 0)
{
columnTitle += "\t";
}
columnTitle += dgv.Columns[i].HeaderText;
}
sw.WriteLine(columnTitle);
//写入列内容
for (int j = 0; j < dgv.Rows.Count; j++)
{
string columnValue = "";
for (int k = 0; k < dgv.Columns.Count; k++)
{
if (k > 0)
{
columnValue += "\t";
}
if (dgv.Rows[j].Cells[k].Value == null)
columnValue += "";
else
columnValue += dgv.Rows[j].Cells[k].Value.ToString().Trim();
}
sw.WriteLine(columnValue);
}
sw.Close();
myStream.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
方法二:包含图片
Microsoft.Office.Interop.Excel.Application Myexcel = new Microsoft.Office.Interop.Excel.Application();
if (Myexcel == null)
{
return;
}
Microsoft.Office.Interop.Excel._Workbook xBk;
xBk = Myexcel.Application.Workbooks.Add(true);
Microsoft.Office.Interop.Excel._Worksheet xSt;
xSt = (Microsoft.Office.Interop.Excel._Worksheet)xBk.ActiveSheet;
//设置标题等
string Title = null;
Title = DateTime.Now.ToLongDateString() + "报价表";
xSt.Name = Title;
//报表的格式设置
//xSt.Cells[1, 6] = Title;
// xSt.get_Range(Myexcel.Cells[1, 6], Myexcel.Cells[1, 6]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
// xSt.get_Range(Myexcel.Cells[1, 6], Myexcel.Cells[1, 6]).Font.Bold = true;
//xSt.get_Range(Myexcel.Cells[1, 6], Myexcel.Cells[1, 6]).Font.Size = 20;
xSt.Cells[1, 1] = "品号";
xSt.Cells[1, 2] = "品名";
xSt.Cells[1, 3] = "客户品号";
xSt.Cells[1, 4] = "图片";
xSt.Cells[1, 5] = "客户编码";
xSt.Cells[1, 6] = "客户名称";
xSt.Cells[1, 7] = "数量";
xSt.Cells[1, 8] = "币种";
xSt.Cells[1, 9] = "汇率";
xSt.Cells[1, 10] = "原币单价";
xSt.Cells[1, 11] = "原币总价";
xSt.Cells[1, 12] = "本币单价";
xSt.Cells[1, 13] = "本币总价";
xSt.Cells[1, 14] = "创建时间";
//下面是用循环把datagridview中的内容写到excel
for (int rowIndex = 0; rowIndex < this.dgvQuotation.Rows.Count; rowIndex++)
{
int colIndex = 0;
for (colIndex = 1; colIndex <= dgvQuotation.ColumnCount; colIndex++)
{
String value = null;
if (dgvQuotation.Rows[rowIndex].Cells[colIndex - 1].Value != null)
{
value = dgvQuotation.Rows[rowIndex].Cells[colIndex - 1].Value.ToString();
if (dgvQuotation.Columns[colIndex - 1].Name == "图片") //处理
{
Image img;
//如果是二进制形式:
//MemoryStream ms = new MemoryStream((byte[])dgvQuotation.Rows[rowIndex].Cells[colIndex - 1].Value);
if (File.Exists(@"D:\产品图片\" + dgvQuotation.Rows[rowIndex].Cells[0].Value.ToString() + ".jpg"))
{
//需要判断是否存在图片
img = Image.FromFile(@"D:\产品图片\" + dgvQuotation.Rows[rowIndex].Cells[0].Value.ToString() + ".jpg");//双引号里是图片的路径
}
else
{
//需要判断是否存在图片
img = Image.FromFile(@"D:\产品图片\LOGO.jpg");//双引号里是图片的路径
}
//Image img = Image.FromStream(ms);
//路径
// Image img = GetImage(value);
string tmpName = tmpPath + "\\temp" + rowIndex + ".bmp";
img.Save(tmpName);
string cellAddr = (GetAddress(rowIndex + 4, colIndex));
Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)xSt.get_Range(cellAddr, Type.Missing);
float left = float.Parse(range.Left.ToString());
float top = float.Parse(range.Top.ToString());
float width = float.Parse(range.Width.ToString());
float height = float.Parse(range.Height.ToString());
xSt.Shapes.AddPicture(tmpName, MsoTriState.msoFalse, MsoTriState.msoTrue, left, top, width, height);
//xSt.Shapes.AddPicture(tmpName, MsoTriState.msoFalse, MsoTriState.msoTrue, left, top, 200, 200);
continue;
}
}
xSt.Cells[rowIndex + 4, colIndex] = value;
}
}
//后台处理
//用户无法看到
Myexcel.Visible = false;
//允许打开对话框保存文件
Myexcel.DisplayAlerts = true;
// xBk.Close(true, "D:\\1.xls", null);
xSt = null;
xBk = null;
Myexcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(Myexcel);
Myexcel = null;
[WinForm]dataGridView导出到EXCEL的更多相关文章
- winform DataGridView 导出到Excel表格 分类: WinForm 2014-07-04 10:48 177人阅读 评论(0) 收藏
public bool ExportDataGridview(DataGridView gridView) { if (gridView.Rows.Count ...
- DataGridView导出到Excel的三个方法
#region DataGridView数据显示到Excel /// <summary> /// 打开Excel并将DataGridView控件中数据导出到Excel /// </s ...
- c# datagridview导出到excel【转载】
c# datagridview导出到excel[转载] http://hi.baidu.com/weizier/blog/item/8212caea1123b4d6d439c9fe.html 本作者使 ...
- C# - VS2019 DataGridView导出到Excel的三种方法
//原文出处:http://www.yongfa365.com/Item/DataGridViewToExcel.html 1 #region DataGridView数据显示到Excel /// & ...
- Winform 中 dataGridView 导出到Excel中的方法总结
最近,在做CS端数据导出到Excel中时网上找了很多代码感觉都不是自己想要的,通过自己的整理归纳得到一个比较通用的方法,就给大家分享一下: 该方法需要用到两个参数(即对象),一个 DataGridV ...
- 【转】c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据
准备工作就是可以分页的DataGridView,和两个按钮,一个用来导出当前页数据到Excel,一个用来导出全部数据到Excel 没有使用SaveFileDialog,但却可以弹出保存对话框来 先做导 ...
- winform datagridview 导出excel
using System;using System.Collections.Generic;using System.Text;using System.IO;using Microsoft.Offi ...
- WinForm中DataGridView导出为Excel(快速版)
public static void ExportExcel(DataGridView myDGV, string fileName) { string saveFileName = fileName ...
- DataGridView 导出到Excel
#region 导出四个表格到Excel /// <summary> /// 导出四个表格到Excel /// </summary> /// <param name=&q ...
随机推荐
- 排序分析函数中对null的处理
--排序分析函数中对null的处理 --分析:对于null在分析函数中是升序默认是nulls last,降序默认是nulls first.如果不指定排序,那么是升序 )); ,'测试1'); ,'测试 ...
- Python File(文件) 方法
file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行读写操作. 2 file.flush() ...
- Python3 多线程
多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹出一个进 ...
- 关于Linux下软件包aptitude的相关操作
aptitude+回车 - 进入aptitude操作界面,可以对预览查看各种软件包 aptitude show package_name - 列出与XXX相关的软件包信息,但是并不能看到该软件包所安装 ...
- 万众瞩目之下,ANGULAR 2终于正式发布啦!
转载:https://angular.io/ 怀着期盼的心情,终于盼到了稳定版本,那么我就可以专心研究了,不再为不定期的修复烦恼咯. 今天,在 Google 总部一个特别的聚会上,我们发布了 Angu ...
- The new powerful SQL executing schedule monthly or weekly in DB Query Analyzer 7.01
1 About DB Query Analyzer DB Query Analyzer is presented by Master Genfeng,Ma from Chinese Mainland. ...
- Python动态展现之一
首先: def f(): print('first') def g(): f() g() def f(): print('second') g() 结果: >>> first sec ...
- 【伯乐在线】HashMap的工作原理
本文由 ImportNew - 唐小娟 翻译自 Javarevisited.欢迎加入翻译小组.转载请见文末要求. HashMap的工作原理是近年来常见的Java面试题.几乎每个Java程序员都知道Ha ...
- Android下实现手机验证码
Android实现验证码 效果图 Github地址 地址:https://github.com/kongqw/Android-CheckView 使用 <kong.qingwei.demo.kq ...
- lucene索引库的增删改查操作
1. 索引库的操作 保持数据库与索引库的同步 说明:在一个系统中,如果索引功能存在,那么数据库和索引库应该是同时存在的.这个时候需要保证索引库的数据和数据库中的数据保持一致性.可以在对数据库进行增.删 ...