这里我们使用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的更多相关文章

  1. 360浏览器下载excel问题解决方式

    亲们有没有碰到过今天我遇到的这件事. 如果使用简单的链接.或者get方式提交的表单,去下载excel,那么360浏览器就会有问题. 问题是:它没把我用java生成的excel表格下载,而是去把我的列表 ...

  2. java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)

    使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...

  3. 在线读取Mongodb数据库下载EXCEL文件

    版本:Mongodb2.4.8 通过页面下载Excel文件 jsp <%@ page language="java" contentType="text/html; ...

  4. angularJS通过post方法下载excel文件

    最近工作中遇到,要使用angularJS的post方法来下载excel的情况.网上找到一个帖子:http://stackoverflow.com/questions/22447952/angularj ...

  5. asp.net 下载Excel (数据流,不保存)--客户端

    效果图: 前端页面 <html> <head> <title>Test For Excel</title> <script src="j ...

  6. Angularjs 通过WebApi 下载excel

    如果想知道 AngularJs 通过WebAPI 下载Excel.请看下文,这里仅提供了一种方案. 服务器端代码如下: protected HttpResponseMessage GenereateE ...

  7. 前端axios下载excel(二进制)

    需求:通过后端接口下载excel文件,后端没有文件地址,返回二进制流文件 实现:axios(ajax类似) 主要代码: axios:设置返回数据格式为blob或者arraybuffer 如: var ...

  8. 前端axios下载excel,并解决axios返回header无法获取所有数据的问题

    需求:通过后端接口下载excel文件,后端没有文件地址,返回二进制流文件 实现:axios(ajax类似) 主要代码: axios:设置返回数据格式为blob或者arraybuffer 如: var ...

  9. C# 之 下载EXCEL文件,自动用迅雷下载aspx

    在浏览器中导出 Excel 得时候,如果浏览器绑定了迅雷,则会下载aspx文件. 解决:下载EXCEL文件,自动用迅雷下载aspx if (Request.QueryString["id&q ...

随机推荐

  1. 库函数atoi()的实现

    int atoi(const char *nptr); 假设第一个非空格字符存在,是数字或者正负号则開始做类型转换,之后检測到非数字(包含结束符 \0) 字符时停止转换.返回整形数. 否则,返回零. ...

  2. 【iOS发展-28】制造业UITabBarController标记控制器、定制UITabBarItem文字图像6途径和More评论

    一个.一个简单的制作过程(实际工程中不建议这样的方式,不要只展示所用原理的理解) 在AppDelegate.m在: - (BOOL)application:(UIApplication *)appli ...

  3. Codeforces Round #254 (Div. 2):A. DZY Loves Chessboard

    A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...

  4. JMeter怎么使用代理服务器

    一.JMeter有一个内置的代理服务器,主要用户使用浏览器录制脚本,在左侧的WorkBench中添加HTTP Proxy Server即可, 其中port表示代理服务器段口号, URL Pattern ...

  5. How to recover from 'programmers burnout(转)

    程序员这个压力大,节奏快,任务繁重,所以很容易令人感觉倦怠,令人感觉烦躁,郁闷,疲惫不堪. 本文将介绍的是程序员如何克服可怕的“职业倦怠”. 丰盛的早餐——身处高科技产业漩涡的我们常常会熬夜到凌晨两三 ...

  6. thinkphp学习笔记1—目录结构和命名规则

    原文:thinkphp学习笔记1-目录结构和命名规则 最近开始学习thinkphp,在下不才,很多的问题看不明白所以想拿出来,恕我大胆发在首页上,希望看到的人能为我答疑解惑,这样大家有个互动,学起来快 ...

  7. 数学思想方法-sasMEMO(17)

    SAS日期及时间格式 data  _null_;input mydate YYMMDD10.;put mydate YYMMDDB10.;put mydate YYMMDDC10.;put mydat ...

  8. Claris and XOR

    Problem Description Claris loves bitwise operations very much, especially XOR, because it has many b ...

  9. Java数据结构与算法(13) - ch06递归(归并排序)

    时间为O(N*logN). 归并排序的一个缺点是它需要在存储器中有另一个大小等于被排序的数据项数目的数组.归并两个有序的数组.利用递归,不断的将数组进行二分法排序,然后进行归并即可.

  10. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(21)-用户角色权限基本的实现说明

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(21)-用户角色权限基本的实现说明     ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇)   (1):框 ...