[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 ...
随机推荐
- js动态加载js css文件,可以配置文件后辍,防止浏览器缓存
js的引用,在浏览器,或微信上访问经常会遇到文件改了,但就是没有更新的问题,使用此函数可以轻松解决缓存问题只需要把js的引用方式改为使用此函数加载即可 源码如下: /** * js动态加载js css ...
- 【python标准库模块二】random模块学习
random模块是用来生成随机数的模块 导入random模块 import random 生成一个0~1的随机数,浮点数 #随机生成一个0~1的随机数 print(random.random()) 生 ...
- Hibernate异常之命名查询节点未找到
异常信息: java.lang.IllegalArgumentException: No query defined for that name [salaryEmps] at org.hiberna ...
- 使用WeihanLi.Redis操作Redis
WeihanLi.Redis Intro StackExchange.Redis 扩展,更简单的泛型操作,并提供一些的适用于业务场景中的扩展 基于 Redis 的五种数据类型扩展出了一些应用: Str ...
- [boost] build boost with intel compiler 16.0.XXX
Introduction There are few information about how to compile boost with Intel compiler. This article ...
- 高通开发笔记---Yangtze worknote
点击打开链接 1. repo init -u git://review.sonyericsson.net/platform/manifest -b volatile-jb-mr1-yangtze 2. ...
- Vc2015 utf8 格式出错
Vc2015 utf8 格式出错(金庆的专栏)用Vc2015新建一个项目,如下添加一行中文注释.然后将这个文件改为utf8无BOM格式,再转换行结束符为Unix格式.#include "st ...
- 【酷我天气】完整的天气App项目
本人完全自主设计与开发的一款轻量级简约好用的天气App,无广告,无烦人的通知栏信息,定位精准,天气信息数据准确,还支持更换背景皮肤哦,颜值爆表. 实现的功能: 1自动定位:自动获取用户所在的城市位置然 ...
- Windows安装和使用fftw
FFTW是一个比较快的.非常出名的一个DFT的开源库. 本文探索安装和配置FFTW,用Visual Studio 2008来使用fftw. 第一步:下载最新的fftw库 这一步很简单,只要在googl ...
- UNIX网络编程——进程间通信概述
一.顺序程序与并发程序特征 顺序程序特征 顺序性封闭性:(运行环境的封闭性)确定性可再现性 并发程序特征 共享性并发性随机性 二.进程互斥 1.由于各进程要求共享资源,而且有些资源需要互斥使用,因此各 ...