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. C# 调用FFmpeg 根据图片合成视频

    1.项目结构: 2.代码: using System; using System.Collections.Generic; using System.Diagnostics; using System ...

  2. 十大面试问题解惑,秒杀一切HR、技术面试

    最能体现求职者能力的就是面试,能不能拿到Offer,取决于你面试时的表现,只有有准备才能在面试过程中游刃有余.小编收集了10个面试官最爱提的问题,虽然题目千变万化,但是万变不离其宗,只要掌握了答题的技 ...

  3. DataTable类

    DataTable是一个使用非常多的类,记得我在刚刚开始学习.Net的时候就已经了解并用过这个类,但如今再来看看,才发现这个类非常之复杂,复杂表现在哪些地方呢?主要是这个类与其他很多类都有关联,也就是 ...

  4. xargs mv命令使用方法:ls *.mp3 |xargs -i mv {} /tmp

    ls  *.mp3 |xargs -i  mv {} /tmp 或者 find . -name "*.mp3" -exec mv {} /tmp \;

  5. ubuntu查看硬件信息

    1,外部探针probe sudo apt-get install hwinfo 执行hwinfo获取系统信息 --short

  6. java_IO流之 NIO

    NIO 定义 即新IO,在JDK1.4的java.nio.*包中引入,其目的在于提高速度. 在Java1.4之前的I/O系统中,提供的都是面向流的I/O系统,系统一次一个字节地处理数据,一个输入流产生 ...

  7. android ANR 案例分析

    案例1:关键词:ContentResolver in AsyncTask onPostExecute, high iowait Process:com.android.email Activity:c ...

  8. 【Oracle】删除重复记录

    --复习autotrace: SET AUTOTRACE OFF --不生成AUTOTRACE 报告,这是缺省模式 SET AUTOTRACE ON EXPLAIN --AUTOTRACE只显示优化器 ...

  9. UGUI实现的虚拟摇杆,可改变摇杆位置

    实现方式主要参考这篇文章:http://www.cnblogs.com/plateFace/p/4687896.html. 主要代码如下: using UnityEngine; using Syste ...

  10. LAMP的安装

    一,LAMP的安装流程:mysql.apache.php或者apache.mysql.php.php放到最后的原因是,php在编译安装的时候是依赖于前2者的. 二,Mysql的安装: 1.下载mysq ...