CreateExcel 导出Excel
public class CreateExcel
{ /// <summary>
/// 用Excel组件导出Excel文件
/// </summary>
/// <param name="path"></param>
/// <param name="datable"></param>
/// <param name="msg"></param>
/// <returns></returns>
public static bool SaveExcel(string path,DataSet datable,ref string msg)
{ Application exlApp = new ApplicationClass(); if (exlApp == null)
{
msg = "Excel 未安装或无法启动!";
return false;
} Workbook exlworke = exlApp.Workbooks.Add(true);
Worksheet exlworkSheet =(Worksheet)exlworke.ActiveSheet; int colunindex = ;
int rowindex = ;
int colunCount = datable.Tables[].Columns.Count;
int rowCount = datable.Tables[].Rows.Count; Range exlrange = exlworkSheet.get_Range(exlApp.Cells[, ], exlApp.Cells[, colunCount]);
exlrange.MergeCells = true;
exlApp.ActiveCell.FormulaR1C1 = "操作人: " + msg + " 导出时间:" + System.DateTime.Now.ToLongDateString();
exlApp.ActiveCell.Font.Size = ;
exlApp.ActiveCell.Font.Bold = true;
exlApp.ActiveCell.HorizontalAlignment = Constants.xlCenter; object[,] obneed = new object[rowCount + , colunCount + ]; foreach (DataColumn dc in datable.Tables[].Columns)
{
obneed[rowindex, colunindex++] = dc.ColumnName;
}
rowindex++; foreach (DataRow dr in datable.Tables[].Rows)
{
for(colunindex=;colunindex<colunCount;colunindex++)
{
obneed[rowindex, colunindex] = dr[colunindex];
}
rowindex++;
} exlrange = exlworkSheet.get_Range(exlApp.Cells[, ], exlApp.Cells[rowCount+, colunCount]);//标题和标头+2
exlrange.Value2 = obneed; try
{
exlworke.Saved = true;
exlworke.SaveCopyAs(path);
}
catch
{
msg = "保存失败!";
}
finally
{
exlApp.Quit();
GC.Collect();
}
msg = "导出成功!";
return true;
}
/// <summary>
/// 使用数据流导出Excel文件
/// </summary>
/// <param name="path"></param>
/// <param name="datable"></param>
/// <param name="msg"></param>
/// <returns></returns>
public static bool SaveIOExcel(string path, DataSet datable, ref string msg)
{ int colunindex = ;
int colunCount = datable.Tables[].Columns.Count;
int rowCount = datable.Tables[].Rows.Count;
try
{
using (FileStream flStream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(flStream, System.Text.Encoding.GetEncoding(-)))
{
StringBuilder strb = new StringBuilder();
for (colunindex = ; colunindex < colunCount; colunindex++)
{
strb.Append(datable.Tables[].Columns[colunindex].ColumnName).Append("\t");
}
strb.AppendLine();
foreach (DataRow dr in datable.Tables[].Rows)
{
for (colunindex = ; colunindex < colunCount; colunindex++)
{
strb.Append(dr[colunindex]).Append("\t");
}
strb.AppendLine();
}
strb.AppendLine("操作人: " + msg + " 导出时间:" + System.DateTime.Now.ToLongDateString());
sw.Write(strb.ToString());
} }
}
catch
{
msg = "转换失败!";
return false;
}
msg = "导出成功!";
return true;
}
}
CreateExcel 导出Excel的更多相关文章
- 导出Excel通用工具类
导出Excel的两种方法: 一,POI 导入poi包 poi-3.11-beta3-20141111.jar /** * */ package com.car.ots.mpckp.utils; imp ...
- Java POI导入导出Excel
1.异常java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException 解决方法: 使用的poi的相关jar ...
- NPOI导出Excel(含有超过65335的处理情况)
NPOI导出Excel的网上有很多,正好自己遇到就学习并总结了一下: 首先说明几点: 1.Excel2003及一下:后缀xls,单个sheet最大行数为65335 Excel2007 单个sheet ...
- 带复杂表头合并单元格的HtmlTable转换成DataTable并导出Excel
步骤: 一.前台JS取HtmlTable数据,根据设定的分隔符把数据拼接起来 <!--导出Excel--> <script type="text/javascript&qu ...
- nodejs 导出excel
nodejs 对查询数据生成excel并下载,采用方式先生成本excel文件,然后再下载:通过比较采用excel-export插件代码如下: excel.js代码: var extend = requ ...
- asp.net中导出Excel的方法
一.asp.net中导出Excel的方法: 本文转载 在asp.net中导出Excel有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出 ...
- [转] Asp.Net 导出 Excel 数据的9种方案
湛刚 de BLOG 原文地址 Asp.Net 导出 Excel 数据的9种方案 简介 Excel 的强大之处在于它不仅仅只能打开Excel格式的文档,它还能打开CSV格式.Tab格式.website ...
- asp.net中导出excel数据的方法汇总
1.由dataset生成 代码如下 复制代码 public void CreateExcel(DataSet ds,string typeid,string FileName) { Htt ...
- ASP.NET导出excel表方法汇总
asp.net里导出excel表方法汇总 1.由dataset生成 public void CreateExcel(DataSet ds,string typeid,string FileName) ...
随机推荐
- fuser
fuser 命令,查看正在被占用的文件:
- erlang热部署
以下流程参考rebar的wiki,亲测 rebar的版本一定要注意,高版本对于下面两个指令有bug rebar generate-appups rebar generate-upgrade 经过一个个 ...
- git在windows上的安装和简单使用
git在windows上的安装和简单使用. 参考: https://git-scm.com/book/zh/v1/Git-%E5%9F%BA%E7%A1%80-%E8%BF%9C%E7%A8%8B%E ...
- AppCan使用注意问题
1.文件上传的时候尽量使用uexUploadMsg,然后注意文件名,文件名一定要正确才能传上去.
- Linux盘符漂移问题
Linux管理多块磁盘时(以sata盘为例),会按磁盘加载的顺序依次给磁盘命名为/dev/sda, /dev/sdb... 这种命名规则就会导致,一块磁盘在发生热插拔或系统重启后,盘符可能发生变化,会 ...
- ITextSharp使用说明 (转)
原文: http://www.cnblogs.com/LifelongLearning/archive/2010/12/28/1919138.html TextSharp是一个生成Pdf文件的开源项目 ...
- thinkphp中配置信息的二维数组设置与使用
有时候配置信息是二维数组 1.配置 <?php return array ( // 阿里大鱼短信配置 'dayu_appkey'=>'xxx', 'dayu_secretKey'=> ...
- 固定容器内任意个div填充算法
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 使用springboot写一个简单的测试用例
使用springboot写一个简单的测试用例 目录结构 pom <?xml version="1.0" encoding="UTF-8"?> < ...
- 不使用sudo命令执行docker
不使用sudo命令执行docker 2015-09-11 11:03:05 王春生 8049 最后编辑:王春生 于 2015-09-11 12:18:30 简介:本篇文章介绍如何不使用sudo命 ...