using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Reflection;
using System.Collections;
using Microsoft.Office.Interop.Excel;
namespace Common
{
public class ExcelExportHelper
{
/// <summary>
/// 集合装换DataTable
/// </summary>
/// <param name="list">集合</param>
/// <returns></returns>
public static System.Data.DataTable ToDataSet(IList p_List)
{
System.Data.DataTable _DataTable = new System.Data.DataTable();
if (p_List.Count > )
{
PropertyInfo[] propertys = p_List[].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
_DataTable.Columns.Add(pi.Name, pi.PropertyType);
}
for (int i = ; i < p_List.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(p_List[i], null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
_DataTable.LoadDataRow(array, true);
}
}
return _DataTable;
}
/// <summary>
/// 分Sheet导出Excel文件
/// </summary>
/// <param name="dv">需导出的DataView</param>
/// <returns>导出文件的路径</returns>
/// <summary>
/// 分Sheet导出Excel文件
/// </summary>
/// <param name="ds">需要导出的数据集 可包含多个Table</param>
/// <param name="fileName">导出的文件名(不能有横线-,也不能有空格)</param>
/// <returns></returns>
public static void DataView2ExcelBySheet(string[] SheetName, DataSet ds, string fileName)
{
GC.Collect();//垃圾回收
Application excel;
_Workbook xBk;
_Worksheet xSt = null;
excel = new ApplicationClass();
xBk = excel.Workbooks.Add(true);
//定义循环中要使用的变量
int rowIndex = ;
int colIndex = ;
int sheetCount = ;
//对全部Sheet进行操作
foreach (System.Data.DataTable dt in ds.Tables)
{
//初始化Sheet中的变量
rowIndex = ;
colIndex = ;
//创建一个Sheet
if (null == xSt)
{
xSt = (_Worksheet)xBk.Worksheets.Add(Type.Missing, Type.Missing, , Type.Missing);
}
else
{
xSt = (_Worksheet)xBk.Worksheets.Add(Type.Missing, xSt, , Type.Missing);
}
//设置Sheet的名称
if (SheetName.Length > )
{
xSt.Name = SheetName[sheetCount - ];
}
//取得标题
foreach (DataColumn col in dt.Columns)
{
//设置标题格式
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter; //设置标题居中对齐
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).Font.Bold = true;//设置标题为粗体
//填值,并进行下一列
excel.Cells[rowIndex, colIndex++] = col.ColumnName;
}
//取得表格中数量
int drvIndex;
for (drvIndex = ; drvIndex <= dt.Rows.Count - ; drvIndex++)
{
DataRow row = dt.Rows[drvIndex];
//新起一行,当前单元格移至行首
rowIndex++;
colIndex = ;
foreach (DataColumn col in dt.Columns)
{
if (col.DataType == System.Type.GetType("System.DateTime"))
{
excel.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
}
else if (col.DataType == System.Type.GetType("System.String"))
{
if (row[col.ColumnName].ToString().Contains("http"))
{
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
Range tempRange = xSt.get_Range(xSt.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]);
string strHyperlinks = row[col.ColumnName].ToString();
xSt.Hyperlinks.Add(tempRange, strHyperlinks, Missing.Value, Missing.Value, Missing.Value);
}
else
{
excel.Cells[rowIndex, colIndex] = "'" + row[col.ColumnName].ToString();
}
}
else
{
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
colIndex++;
}
}
//使用最佳宽度
Range allDataWithTitleRange = xSt.get_Range(excel.Cells[, ], excel.Cells[rowIndex, colIndex - ]);
allDataWithTitleRange.Select();
allDataWithTitleRange.Columns.AutoFit();
allDataWithTitleRange.Borders.LineStyle = ;//将导出Excel加上边框
sheetCount++;
}
//设置导出文件在服务器上的文件夹
string exportDir = "~/ExcelFile/";//注意:该文件夹您须事先在服务器上建好才行
string strPath = System.IO.Path.Combine(exportDir, fileName);
//设置文件在服务器上的路径
string absFileName = HttpContext.Current.Server.MapPath(exportDir) + fileName;
xBk.SaveCopyAs(absFileName);
xBk.Close(false, null, null);
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();
HttpResponse resp;
resp = System.Web.HttpContext.Current.Response;
resp.Charset = "GB2312";
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", ("attachment;filename=" + fileName));
resp.WriteFile(absFileName, false);
resp.End(); }
}
}
------------------------------
/// <summary>
/// 文件查询页导出功能
/// </summary>
public void SelectDoc_Export2(string data)
{
var input = data.DeserializeObject<StructSelectDoc>();
using (var context = DOCDBHelper.DataContext)
{
var results = context.Usp_DOC_SelectDoc_Export(input.docNo, input.docName, input.docFlag,
input.docCatagoryID, input.docSenderName, input.docSenderDep, input.docRecvDepName,
input.createDate1, input.createDate2);
List<Usp_DOC_SelectDoc_ExportResult1> revList = results.GetResult<Usp_DOC_SelectDoc_ExportResult1>().ToList();
List<Usp_DOC_SelectDoc_ExportResult2> sendList = results.GetResult<Usp_DOC_SelectDoc_ExportResult2>().ToList();
DataSet ds = new DataSet();
if (revList != null && revList.Count > )
{
ds.Tables.Add(CommonUtil.ListToDataTable(revList));
}
if (sendList != null && sendList.Count > )
{
ds.Tables.Add(CommonUtil.ListToDataTable(sendList));
}
if (ds.Tables.Count > )
{
string fileName = "DocExport.csv";
string[] sheetName = new string[] { "收文", "发文" };
ExcelExportHelper.DataView2ExcelBySheet(sheetName, ds, fileName);
}
}
}

c# excel sheep 导出的更多相关文章

  1. 【基于WinForm+Access局域网共享数据库的项目总结】之篇二:WinForm开发扇形图统计和Excel数据导出

    篇一:WinForm开发总体概述与技术实现 篇二:WinForm开发扇形图统计和Excel数据导出 篇三:Access远程连接数据库和窗体打包部署 [小记]:最近基于WinForm+Access数据库 ...

  2. C# 之 EXCEL导入导出

    以下方式是本人总结的一些经验,肯定有很多种方法,在此先记下,留待以后补充... 希望朋友们一起来探讨相关想法,请在下方留言. A-1:EXCEL模板导出 非常简单,将EXCEL模板上传到项目中后,将其 ...

  3. 利用反射实现通用的excel导入导出

    如果一个项目中存在多种信息的导入导出,为了简化代码,就需要用反射实现通用的excel导入导出 实例代码如下: 1.创建一个 Book类,并编写set和get方法 package com.bean; p ...

  4. Excel导入导出的业务进化场景及组件化的设计方案(上)

    1:前言 看过我文章的网友们都知道,通常前言都是我用来打酱油扯点闲情的. 自从写了上面一篇文章之后,领导就找我谈话了,怕我有什么想不开. 所以上一篇的(下)篇,目前先不出来了,哪天我异地二次回忆的时候 ...

  5. java实现excel模板导出

    一. 准备工作 1. 点击此下载相关开发工具 2. 将poi-3.8.jxls-core-1.0两个jar包放到工程中,并引用 3. 将excel模板runRecord.xls放到RunRecordB ...

  6. Excel导入-----导出(包含所选和全部)操作

    在做系统的时候,很多时候信息量太大,这时候就需要进行Excel表格信息的导入和导出,今天就来给大家说一下我使用Excel表格信息导入和导出的心得. 1:首先需要在前端显示界面View视图中添加导入Ex ...

  7. C#实现Excel模板导出和从Excel导入数据

    午休时间写了一个Demo关于Excel导入导出的简单练习 1.窗体 2.引用office命名空间 添加引用-程序集-扩展-Microsoft.Office.Interop.Excel 3.封装的Exc ...

  8. 如何将jsp页面的table报表转换到excel报表导出

    假设这就是你的jsp页面: 我们会添加一个“导出到excel”的超链接,它会把页面内容导出到excel文件中.那么这个页面会变成这个样子 在此,强调一下搜索时关键词的重要性,这样一下子可以定位到文章, ...

  9. 关于Excel导入导出的用例设计

    目前,为方便操作,很多系统都会增加批量导入导出的功能.文件导入导出一般格式都是excel.由于用户直接在excel在填写内容,无法控制填写的格 式,加上excel解析比较困难,所以一般涉及到excel ...

随机推荐

  1. 对$NOMOD51的理解

    很多朋友在看asm代码的时候,对下面的语句不是很了解,下面解说一下. $NOMOD51 $INCLUDE (REG932.INC) 解释:$NOMOD51,这一指令功能是使A51不识别8051的所有预 ...

  2. Linux下的摄影后期处理软件

    由于喜欢摄影,在LInux上折腾,想找一款能代替lightroom的软件.发现darktable这款软件专业.于是就安装了. 以下是在Linux上安装darktable的instruction,需要添 ...

  3. UESTC_酱神的旅行 2015 UESTC Training for Dynamic Programming<Problem M>

    M - 酱神的旅行 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit ...

  4. LeeCode-Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. int mySqrt(int x) { ) ; /* for(i ...

  5. UVA 100 - The 3n+1 problem (3n+1 问题)

    100 - The 3n+1 problem (3n+1 问题) /* * 100 - The 3n+1 problem (3n+1 问题) * 作者 仪冰 * QQ 974817955 * * [问 ...

  6. Java程序员面试题集(1-50)(转)

    转:http://blog.csdn.net/jackfrued/article/details/17339393 下面的内容是对网上原有的Java面试题集及答案进行了全面修订之后给出的负责任的题目和 ...

  7. hpux操作系统的关机与重新启动命令

    关机  shutdown -hy 0 重新启动: shutdown -ry 0

  8. jQuery支持移动Mobile的DOM元素移动和缩放插件

    jQuery Panzoom是一款很有用的HTML DOM元素平移和缩放jQuery和CSS3插件. Panzoom利用CSS transforms 和 matrix函数来为浏览器进行硬件(GPU)加 ...

  9. 更新-----Scripts:执行双网卡绑定

    #!/bin/bash #------------------------------------------------------------------------------- # Name: ...

  10. mysql binlog参数设置

    1.mysql有许多系统变量,可以设置,系统变量设置不同,不同的系统将导致执行状态. 故mysql提供两组命令,分别查看系统设置和执行状态. 1.系统设置: SHOW [GLOBAL | SESSIO ...