ExcelHelper office 导出
要是服务器上没有装着excel 可以用c#导出excel表吗
租用的空间 服务器上没有装着office excel,可以用c#导出excel表吗?
怎样做?给点代码……
可以使用二进制读写Excel的开源组件org.in2bits.MyXls.dll导出真正的Excel文件。 以下是我简单封装的一个ExcelHelper using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Collections.Generic; using org.in2bits.MyXls; using org.in2bits.MyXls.ByteUtil; /// <summary> ///ExcelHelper 的摘要说明 /// </summary> public class ExcelHelper { protected ExcelHelper() { } public class TableStruct { private string _TableName; private string _TableCaption; private List<ColumnStruct> _ColumnInfoAry; public string TableName { get { if (string.IsNullOrEmpty(_TableName)) { return string.Empty; } return _TableName; } set { _TableName = value; } } public string TableCaption { get { if (string.IsNullOrEmpty(_TableCaption)) { return TableName; } return _TableCaption; } set { _TableCaption = value; } } public List<ColumnStruct> ColumnInfoAry { get { if (_ColumnInfoAry == null) { _ColumnInfoAry = new List<ColumnStruct>(); } return _ColumnInfoAry; } } public void AddColumnInfo(ColumnStruct ColumnInfo) { if (ColumnInfo == null) { return; } if (_ColumnInfoAry == null) { _ColumnInfoAry = new List<ColumnStruct>(); } foreach (ColumnStruct col in _ColumnInfoAry) { if (col.ColumnName.Equals(ColumnInfo.ColumnName, StringComparison.OrdinalIgnoreCase)) { return; } } _ColumnInfoAry.Add(ColumnInfo); } public ColumnStruct GetColumnInfo(string ColumnName) { if (string.IsNullOrEmpty(ColumnName) ) { return null; } if (_ColumnInfoAry == null) { return null; } ColumnStruct ColumnInfo = null; foreach (ColumnStruct col in _ColumnInfoAry) { if (col.ColumnName.Equals(ColumnName, StringComparison.OrdinalIgnoreCase)) { ColumnInfo = col; } } return ColumnInfo; }
} public class ColumnStruct { private string _ColumnName; private string _ColumnCaption; private string _ColumnTextFormat;
public string ColumnName { get { if (string.IsNullOrEmpty(_ColumnName)) { return string.Empty; } return _ColumnName; } set { _ColumnName = value; } } public string ColumnCaption { get { if (string.IsNullOrEmpty(_ColumnCaption)) { return ColumnName; } return _ColumnCaption; } set { _ColumnCaption = value; } }
public string ColumnTextFormat { get { if (string.IsNullOrEmpty(_ColumnTextFormat)) { return string.Empty; } return _ColumnTextFormat; } set { _ColumnTextFormat = value; } } }
public static void ExportToExcel(DataSet ds,List<TableStruct> TableInfoAry) { if (ds == null) { ds = new DataSet(); } if ( TableInfoAry == null ) { TableInfoAry = new List<TableStruct>(); }
XlsDocument xls = new XlsDocument(); xls.FileName = DateTime.Now.ToString("yyyyMMddHHmmssffff",System.Globalization.DateTimeFormatInfo.InvariantInfo); xls.SummaryInformation.Author = "wangmh"; //填加xls文件作者信息 xls.SummaryInformation.NameOfCreatingApplication = "Microsoft Excel"; //填加xls文件创建程序信息 xls.SummaryInformation.LastSavedBy = "wangmh"; //填加xls文件最后保存者信息 xls.SummaryInformation.Comments = "Gwm"; //填加xls文件作者信息 xls.SummaryInformation.Title = "Gwm"; //填加xls文件标题信息 xls.SummaryInformation.Subject = "Gwm";//填加文件主题信息 xls.DocumentSummaryInformation.Company = "Gwm";//填加文件公司信息 foreach (TableStruct TableInfo in TableInfoAry) { DataTable dt = ds.Tables[TableInfo.TableName]; if (dt == null) { continue; } Worksheet sheet = xls.Workbook.Worksheets.Add(TableInfo.TableCaption); //设置标头栏 ushort ColumnIndex = 1; foreach (ColumnStruct ColStruct in TableInfo.ColumnInfoAry) { ushort RowIndex = 1; Row row = sheet.Rows.AddRow(RowIndex); if (!dt.Columns.Contains(ColStruct.ColumnName)) { continue; }
Cell cell = null; if (row.CellExists(ColumnIndex)) { cell = row.GetCell(ColumnIndex); } else { cell = sheet.Cells.Add(RowIndex, ColumnIndex, null); //row.AddCell(cell); } cell.Value = ColStruct.ColumnCaption; cell.Font.Weight = FontWeight.Bold; cell.HorizontalAlignment = HorizontalAlignments.Centered; cell.BottomLineStyle = 2; cell.BottomLineColor = Colors.Grey; cell.Font.Height = 10 * 20; cell.VerticalAlignment = VerticalAlignments.Centered;
ushort ColumnMaxLength = GetColumnValueMaxLength(dt, ColStruct); //设定列宽为自适应宽度 ColumnInfo colInfo = new ColumnInfo(xls, sheet);//生成列格式对象 //设定colInfo格式的起作用的列为第1列到第5列(列格式为0-base) colInfo.ColumnIndexStart = (ushort)(ColumnIndex-1); colInfo.ColumnIndexEnd = colInfo.ColumnIndexStart; colInfo.Width = (ushort)(ColumnMaxLength * 256);//列的宽度计量单位为 1/256 字符宽 sheet.AddColumnInfo(colInfo);//把格式附加到sheet页上(注:AddColumnInfo方法有点小问题,不能把colInfo对象多次附给sheet页)
ColumnIndex++; }
for (ushort i = 0; i < dt.Rows.Count; i++) { ushort RowIndex = (ushort)(i + 2); Row row = sheet.Rows.AddRow(RowIndex); int j = 0; foreach (ColumnStruct ColStruct in TableInfo.ColumnInfoAry) { if ( !dt.Columns.Contains(ColStruct.ColumnName) ) { continue; }
ColumnIndex = (ushort)(j + 1); Cell cell = null; if (row.CellExists(ColumnIndex)) { cell = row.GetCell(ColumnIndex); } else { cell = sheet.Cells.Add(RowIndex, ColumnIndex, null); //row.AddCell(cell); }
object objValue = dt.Rows[i][ColStruct.ColumnName]; cell.Value = GetColumnValueFormat(dt,ColStruct,objValue); cell.Font.Weight = FontWeight.Normal; cell.HorizontalAlignment = HorizontalAlignments.Centered; j++; } } } xls.Send(); }
private static string GetColumnValueFormat(DataTable dt, ColumnStruct ColStruct, Object ObjValue) { string ColumnValue = string.Empty; if ( ObjValue != null && ObjValue!= DBNull.Value ) { string ColumnDataType = dt.Columns[ColStruct.ColumnName].DataType.ToString(); switch (ColumnDataType) { case "System.Boolean": case "System.Byte": { ColumnValue = ObjValue.ToString(); break; } case "System.Decimal": { if (string.IsNullOrEmpty(ColStruct.ColumnTextFormat)) { ColumnValue = Convert.ToDecimal(ObjValue).ToString(); } else { ColumnValue = Convert.ToDecimal(ObjValue).ToString(ColStruct.ColumnTextFormat); } break; } case "System.Double": { if (string.IsNullOrEmpty(ColStruct.ColumnTextFormat)) { ColumnValue = Convert.ToDouble(ObjValue).ToString(); } else { ColumnValue = Convert.ToDouble(ObjValue).ToString(ColStruct.ColumnTextFormat); } break; } case "System.Int64": { if (string.IsNullOrEmpty(ColStruct.ColumnTextFormat)) { ColumnValue = Convert.ToInt64(ObjValue).ToString(); } else { ColumnValue = Convert.ToInt64(ObjValue).ToString(ColStruct.ColumnTextFormat); } break; } case "System.Int16": { if (string.IsNullOrEmpty(ColStruct.ColumnTextFormat)) { ColumnValue = Convert.ToInt16(ObjValue).ToString(); } else { ColumnValue = Convert.ToInt16(ObjValue).ToString(ColStruct.ColumnTextFormat); } break; } case "System.Int32": { if (string.IsNullOrEmpty(ColStruct.ColumnTextFormat)) { ColumnValue = Convert.ToInt32(ObjValue).ToString(); } else { ColumnValue = Convert.ToInt32(ObjValue).ToString(ColStruct.ColumnTextFormat); } break; } case "System.DateTime": { if (string.IsNullOrEmpty(ColStruct.ColumnTextFormat)) { ColumnValue = Convert.ToDateTime(ObjValue).ToString(); } else { ColumnValue = Convert.ToDateTime(ObjValue).ToString(ColStruct.ColumnTextFormat,System.Globalization.DateTimeFormatInfo.InvariantInfo); } break; } default: { ColumnValue = ObjValue.ToString(); break; } } } return ColumnValue.Trim(); }
private static ushort GetColumnValueMaxLength(DataTable dt, ColumnStruct ColStruct) { ushort InitLenth = (ushort)System.Text.Encoding.Default.GetByteCount(ColStruct.ColumnCaption); ushort MaxLenth = InitLenth; foreach (DataRow Row in dt.Rows) { object ObjValue = Row[ColStruct.ColumnName]; if (ObjValue == null || ObjValue == DBNull.Value) { continue; } string ColumnValue = GetColumnValueFormat(dt,ColStruct,ObjValue); ushort ColumnValueLenth = (ushort)System.Text.Encoding.Default.GetByteCount(ColumnValue); MaxLenth = ColumnValueLenth > MaxLenth ? ColumnValueLenth : MaxLenth; }
if (MaxLenth == InitLenth) { //标题栏字体较大 MaxLenth += 4; } else { //内容文本前后与边框空余出一字的间隔 MaxLenth += 2; } return (ushort)(MaxLenth * 1.2); } }
页面调用ExcelHelper类中的静态方法ExportDataToExcel导出数据集中指定Table的制定列到Excel文件并输出文件到浏览器客户端。XmlManager.GetString是多语言实现的相关方法,可以直接修改为返回当前列标题字符串。 private void ExportDataToExcel(DSSummary_DetailsQuery_SellInfo ds) { System.Collections.Generic.List<ExcelHelper.TableStruct> TableInfoAry = new System.Collections.Generic.List<ExcelHelper.TableStruct>(); ExcelHelper.TableStruct TableInfo = new ExcelHelper.TableStruct(); TableInfo.TableName = ds.DS_Stock.TableName; TableInfo.TableCaption = XmlManager.GetString("Summary_DetailsQuery_ViewCarInfoCustormerDetail_TitleTxt");
ExcelHelper.ColumnStruct ColStruct = new ExcelHelper.ColumnStruct(); ColStruct.ColumnName = "C_CarNo"; ColStruct.ColumnTextFormat = string.Empty; ColStruct.ColumnCaption = XmlManager.GetString("IAMS_System_CarInfo_CarNo"); TableInfo.AddColumnInfo(ColStruct); //……添加其他列信息 TableInfoAry.Add(TableInfo); ExcelHelper.ExportToExcel(ds, TableInfoAry); } 希望可以帮到你,有什么问题可以联系我。Email:wangminghu2000@163.com
ExcelHelper office 导出的更多相关文章
- .net 开源工具集1 (SQL,报表,Cube,Office导出)
http://www.cnblogs.com/asxinyu/p/dotnet_opensource_project_3.html
- office 导出问题
就用程序池右击项目高级设置 应用程序池的项目中的标识改为 LocalSystem 启用32位应用程序设为true或false
- C#基于Office组件操作Excel
1. 内容简介 实现C#与Excel文件的交互操作,实现以下功能: a) DataTable 导出到 Excel文件 b) Model数据实体导出到 Excel文件[List&l ...
- 【netcore基础】.NET Core使用EPPlus实现MVC API里的Excel导出功能 配置中文表头
EPPlus 用来操作excel非常方便,不用依赖微软的office包,所以推荐使用. 下面是具体步骤和代码 首先用nuget安装 EPPlus.Core 我装的版本是 1.5.4 然后就可以用 Ex ...
- C# Excel操作类 ExcelHelper
实现C#与Excel文件的交互操作,实现以下功能: 1.DataTable 导出到 Excel文件 2.Model数据实体导出到 Excel文件[List<Model>] 3.导出数据到模 ...
- [.Net] C# Excel操作类 ExcelHelper
实现C#与Excel文件的交互操作,实现以下功能: 1.DataTable 导出到 Excel文件 2.Model数据实体导出到 Excel文件[List<Model>] 3.导出数据到模 ...
- MVC NPOI Linq导出Excel通用类
之前写了一个模型导出Excel通用类,但是在实际应用中,可能不是直接导出模型,而是通过Linq查询后获取到最终结果再导出 通用类: public enum DataTypeEnum { Int = , ...
- Excel导入导出帮助类
/// <summary> /// Excel导入导出帮助类 /// 记得引入 NPOI /// 下载地址 http://npoi.codeplex.com/rele ...
- php 基于phpexcel制作的导出excel类
最近要写个项目,由于项目中导出excel太多,因此写个类便于使用. <?php /**如果是gbk的编码,请转为UTF8 *@使用方法. *引入类库. * $excel = news excel ...
随机推荐
- struts2官方 中文教程 系列一:创建一个struts2 web Application
先贴了本帖地址,以免被爬 http://www.cnblogs.com/linghaoxinpian/p/6898779.html 本教程将会通过安装struts2框架来创建一个简单的应用程序.虽然 ...
- 【bzoj2437】[Noi2011]兔兔与蛋蛋 二分图最大匹配+博弈论
Description Input 输入的第一行包含两个正整数 n.m. 接下来 n行描述初始棋盘.其中第i 行包含 m个字符,每个字符都是大写英文字母"X".大写英文字母&quo ...
- javascript中构造器(函数)的__proto__与prototype初探
背景:最近没什么需求,快要闲出屁了,所以重温了一下js的原型,结果大有收获,且偶然看到Snandy大神的<JavaScript中__proto__与prototype的关系> 这篇文章,感 ...
- SJTU 机试 最小面积子矩阵 压缩+双指针
链接:https://www.nowcoder.com/questionTerminal/8ef506fbab2742809564e1a288358554来源:牛客网 一个N*M的矩阵,找出这个矩阵中 ...
- Windows Server 2008 R2中IIS7.5配置完网站权限不足问题的解决办法:
Windows Server 2008 R2中IIS7.5配置完网站权限不足问题的解决办法:常见问题:HTTP 错误 500.0 - Internal Server Error无法显示页面,因为发生内 ...
- 二分答案 & 洛谷 P2678 跳石头
首先让我们先学一下二分答案这个东西... 二分答案,肯定与二分有关,还与可能是答案的东西有关... 二分答案的准确定义: 二分答案是指在答案具有单调性的前提下,利用二分的思想枚举答案,将求解问题转 ...
- File 文件操作类 大全
File 文件操作类 大全 许多人都会对文件操作感到很难 我也是 但是一个好的项目中必定会涉及到文件操作的 文件的复制 粘贴 等等等 公司大佬写了 一个文件操作的工具类 感觉还是棒棒的啦 ...
- tar命令加密压缩/解密解压
在tar解压文件时发生下面错误信息 gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not rec ...
- windows cmd下创建虚拟环境virtualenv
一:虚拟环境virtualenv 如果在一台电脑上, 想开发多个不同的项目, 需要用到同一个包的不同版本, 如果使用上面的命令, 在同一个目录下安装或者更新, 新版本会覆盖以前的版本, 其它的项目就无 ...
- P3813 [FJOI2017]矩阵填数
传送门 矩阵很大,但是发现 $n$ 很小,从这边考虑,对于一个一堆小矩阵放在一起的情况 考虑把每一块单独考虑然后方案再乘起来 但是这些奇怪的东西很不好考虑 所以暴力一点,直接拆成一个个小块 但是这样我 ...