使用DateSet下载Excel
这里我们使用Microsoft.Office.Interop.Excel.dll下载Excel,没有引用可点击下载
关键代码,ExcelHelper类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO; namespace ExcelDownLoad
{
public delegate void ExcelEventHandler(); public class ExcelHelper:ISubject
{
string processStr = "";
//public Label label2; public string ProcessStr
{
get { return processStr; }
set { processStr = value; }
} #region Kill Special Excel Process [DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); public void KillSpecialExcel(Microsoft.Office.Interop.Excel.Application m_objExcel)
{
try
{
if (m_objExcel != null)
{
int lpdwProcessId;
GetWindowThreadProcessId(new IntPtr(m_objExcel.Hwnd), out lpdwProcessId); System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
}
}
catch (Exception ex)
{
Console.WriteLine("Delete Excel Process Error:" + ex.Message);
}
} #endregion public void ExportExcel(DataSet ds, string saveFileName)
{
if (ds == null) return; bool fileSaved = false; if (saveFileName.IndexOf(":") < ) return; //被点了取消
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,您的电脑未安装Excel"); return;
}
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[];
//取得sheet1
Microsoft.Office.Interop.Excel.Range range;
string oldCaption = DateTime.Today.ToString("yy-MM-dd");
long totalCount = ds.Tables[].Rows.Count;
long rowRead = ;
float percent = ;
//worksheet.Cells[1, 1] = DateTime.Today.ToString("yy-MM-dd");
//写入字段
for (int i = ; i < ds.Tables[].Columns.Count; i++)
{
worksheet.Cells[, i + ] = ds.Tables[].Columns[i].ColumnName;
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[, i + ];
range.Interior.ColorIndex = ;
range.Font.Bold = true;
}
//写入数值
for (int r = ; r < ds.Tables[].Rows.Count; r++)
{
for (int i = ; i < ds.Tables[].Columns.Count; i++)
{
worksheet.Cells[r + , i + ] = ds.Tables[].Rows[r][i];
}
rowRead++;
percent = ((float)( * rowRead)) / totalCount;
//if (label2 != null)
//{
// label2.Text = "正在导出数据[" + percent.ToString("0.00") + "%]..."; //这里可以自己做一个label用来显示进度.
//}
System.Windows.Forms.Application.DoEvents();
}
////this.lbl_process.Visible = false;
range = worksheet.get_Range(worksheet.Cells[, ], worksheet.Cells[ds.Tables[].Rows.Count + , ds.Tables[].Columns.Count]);
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
if (ds.Tables[].Columns.Count > )
{
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
}
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName); fileSaved = true;
}
catch (Exception ex)
{
fileSaved = false; MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}
}
else
{
fileSaved = false;
}
xlApp.Quit();
GC.Collect();//强行销毁
if (fileSaved && File.Exists(saveFileName))
{
//System.Diagnostics.Process.Start(saveFileName);
MessageBox.Show("导出成功!", "通知");
}
} #region ISubject Members public event ExcelEventHandler Update;
public void Notify()
{
if (Update != null)
{
// 使用事件来通知给订阅者
Update();
}
} #endregion
}
}
使用DateSet下载Excel的更多相关文章
- 360浏览器下载excel问题解决方式
亲们有没有碰到过今天我遇到的这件事. 如果使用简单的链接.或者get方式提交的表单,去下载excel,那么360浏览器就会有问题. 问题是:它没把我用java生成的excel表格下载,而是去把我的列表 ...
- java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)
使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...
- 在线读取Mongodb数据库下载EXCEL文件
版本:Mongodb2.4.8 通过页面下载Excel文件 jsp <%@ page language="java" contentType="text/html; ...
- angularJS通过post方法下载excel文件
最近工作中遇到,要使用angularJS的post方法来下载excel的情况.网上找到一个帖子:http://stackoverflow.com/questions/22447952/angularj ...
- asp.net 下载Excel (数据流,不保存)--客户端
效果图: 前端页面 <html> <head> <title>Test For Excel</title> <script src="j ...
- Angularjs 通过WebApi 下载excel
如果想知道 AngularJs 通过WebAPI 下载Excel.请看下文,这里仅提供了一种方案. 服务器端代码如下: protected HttpResponseMessage GenereateE ...
- 前端axios下载excel(二进制)
需求:通过后端接口下载excel文件,后端没有文件地址,返回二进制流文件 实现:axios(ajax类似) 主要代码: axios:设置返回数据格式为blob或者arraybuffer 如: var ...
- 前端axios下载excel,并解决axios返回header无法获取所有数据的问题
需求:通过后端接口下载excel文件,后端没有文件地址,返回二进制流文件 实现:axios(ajax类似) 主要代码: axios:设置返回数据格式为blob或者arraybuffer 如: var ...
- C# 之 下载EXCEL文件,自动用迅雷下载aspx
在浏览器中导出 Excel 得时候,如果浏览器绑定了迅雷,则会下载aspx文件. 解决:下载EXCEL文件,自动用迅雷下载aspx if (Request.QueryString["id&q ...
随机推荐
- 把自解压的RAR压缩包解压到指定的软件安装目录
原文 把自解压的RAR压缩包解压到指定的软件安装目录 今天千里独行同学给轻狂来信问了一个问题:如何把一个自解压的RAR压缩包解压到我们指定的软件安装目录. 其实,在NSIS中,我们可以灵活运用相关 ...
- Scala刮:使用Intellij IDEA写hello world
介绍 在前面的文章中,,我们介绍了如何使用Scala IDE那是,eclipse集成Scala开发插件Scala开发语言程序.使用一段时间后,.发现eclipse正确Scala支持不是很好.用户体验差 ...
- 皴linux rootpassword(方式:重置rootpassword)
皴linux rootpassword: 开机后,.点击"e"进入维护模式.选"内核选项",例如,看到下面的数字: watermark/2/text/aHR0c ...
- HTTP Cookie深入理解
HTTP Cookie 概述:Cookie通常也叫做网站cookie,浏览器cookie或者http cookie,是保存在用户浏览器端的,并在发出http请求时会默认携带的一段文本片段.它可以用来做 ...
- Log4net 日志
Log4net 日志使用介绍 概述 Log4net 有三个主要组件:loggers,appenders 和 layouts.这三个组件一起工作使得开发者能够根据信息类型和等级(Level)记录信息,以 ...
- Hbase在的应用经验的统计
1. 需求统计 互联网上对于数据的统计,一个重要的应用就是对站点站点数据的统计,比如CNZZ站长统计.百度统计.Google Analytics.量子恒道统计等等. 站点站点统计工具无外乎有下面一些功 ...
- Helloworld with c
CentOS 7 之Helloworld with c 其实我也不知道是为了啥, 到了现在这种年纪还想学习Linux下的C语言编程.因为我一直就傻傻地认为机会是垂青有准备的人,也一直呆呆地认为活到 ...
- table中的边框合并实例
<html><head><style type="text/css">table,th,td{border:1px solid blue;bor ...
- linux_安装 redis
Installation Download, extract and compile Redis with: $ wget http://download.redis.io/releases/redi ...
- 在ubuntu上部署hadoop时出现的问题
1. 配置ssh登录 不须要改动/etc/ssh/sshd_config 2. 新建hadoop用户时,home以下没有hadoop文件夹 用以下命令创建 useradd -m hadoop 3. n ...