110,536
社区成员
发帖
与我相关
我的任务
分享
#region 属性
/// <summary>
/// 行索引(表示从某行开始打印,如0表示从第一行开始)
/// </summary>
public virtual int RowIndex
{
get { return _rowindex; }
set
{
_rowindex = _saveindex = value;
}
}
/// <summary>
/// 标题
/// </summary>
public virtual string Title
{
get { return _title; }
set
{
_title = value;
IsDrawTitle = !string.IsNullOrEmpty(value);
}
}
/// <summary>
/// 头文本
/// </summary>
public virtual string HeaderText
{
get { return _headerdtext; }
set
{
_headerdtext = value;
IsDrawHeader = !string.IsNullOrEmpty(value);
}
}
/// <summary>
/// 脚文本
/// </summary>
public virtual string FooterText
{
get { return _footertext; }
set
{
_footertext = value;
IsDrawFooter = !string.IsNullOrEmpty(value);
}
}
/// <summary>
/// 保存地址
/// </summary>
public virtual string SavePath
{
get { return _savepath; }
set
{
_savepath = value;
}
}
/// <summary>
/// 标题字体
/// </summary>
public virtual System.Drawing.Font TitleFont
{
get { return _titlefont; }
set { _titlefont = value; }
}
/// <summary>
/// Header字体
/// </summary>
public virtual System.Drawing.Font HeaderFont
{
get { return _headerfont; }
set { _headerfont = value; }
}
/// <summary>
/// 页脚字体
/// </summary>
public virtual System.Drawing.Font FooterFont
{
get { return _footerfont; }
set { _footerfont = value; }
}
/// <summary>
/// 正文标题字体
/// </summary>
public virtual System.Drawing.Font BodyHeaderFont
{
get { return _bodyheaderfont; }
set { _bodyheaderfont = value; }
}
/// <summary>
/// 正文字体
/// </summary>
public virtual System.Drawing.Font BodyFont
{
get { return _bodyfont; }
set { _bodyfont = value; }
}
/// <summary>
/// 导出表集合
/// </summary>
public virtual List<System.Data.DataTable> Tables
{
get { return _tables; }
set { _tables = value; }
}
/// <summary>
/// 列宽集合(A:A 表示第一列,A:B表示第一二列)
/// </summary>
public virtual Dictionary<string, float> ColumnsWidth
{
get { return _columnswidth; }
set { _columnswidth = value; }
}
/// <summary>
/// 那些列显示边框
/// </summary>
public virtual Dictionary<string, BorderWeightType> ColumnsBorder
{
get { return _columnsborder; }
set { _columnsborder = value; }
}
public virtual bool IsDrawTitle { get; set; }
public virtual bool IsDrawHeader { get; set; }
public virtual bool IsDrawFooter { get; set; }
public virtual TextAlign DrawFooterTextAlign
{
get { return _drawfootertextalign; }
set { _drawfootertextalign = value; }
}
/// <summary>
/// 是否显示数据列表标题
/// </summary>
public virtual bool IsBodyListHeader
{
get { return _isbodylistheader; }
set { _isbodylistheader = value; }
}
/// <summary>
/// 正文(列表)是否显示边框
/// </summary>
public virtual bool IsDispalyBorderAll
{
get { return _isalldisplayborder; }
set { _isalldisplayborder = value; }
}
/// <summary>
/// 是否自动转换成文本格式
/// </summary>
public virtual bool IsAutoConvertText
{
get { return _isautoconverttext; }
set
{
_isautoconverttext = value;
}
}
/// <summary>
/// 标题后面是否追加SheetName
/// </summary>
public virtual bool IsTitleAppendSheetName
{
get { return _isTitleAppendSheetName; }
set
{
_isTitleAppendSheetName = value;
}
}
/// <summary>
/// 正文(列表)边框类型
/// </summary>
public virtual BorderWeightType BorderWeight
{
get { return _borderweight; }
set { _borderweight = value; }
}
/// <summary>
/// 单元格是否自动适应宽度
/// </summary>
public virtual bool IsAutoFit { get; set; }
/// <summary>
/// 单元格是否自动换行
/// </summary>
public virtual bool IsWrapText
{
get { return _iswraptext; }
set { _iswraptext = value; }
}
/// <summary>
/// 条码文本(长度不能超过8位,超过8位请更新图片模版,否则打印出界)
/// </summary>
public string BarCodeText
{
get { return _BarCodeText; }
set
{
_BarCodeText = value;
if (!String.IsNullOrEmpty(value))
{
IsDrawTitle = true;
}
else
{
IsDrawTitle = !string.IsNullOrEmpty(_title);
}
}
}
/// <summary>
/// 设置打印时页面边距(程序中和Excel中的边距单元不一样(Excel设置0.5大概有10px,所以默认10px))
/// </summary>
public PaddingF PageMargin
{
get { return _pageMargin; }
set
{
_pageMargin = value;
}
}
/// <summary>
/// 是否打印页脚(只跟打印有关)
/// </summary>
public bool IsPrintFooter
{
get { return _isPrintFooter; }
set
{
_isPrintFooter = value;
}
}
/// <summary>
/// 要扩大的列(打印时,如果设置了列宽自动适应,有可能总列宽缩小或放大,所以缩小或放大的宽度要算在ExpandColumnName列上)
/// 格式 A:A B:B C:C
/// </summary>
public string ExpandColumnName
{
get { return _expandColumnName; }
set { _expandColumnName = value; }
}
#endregion
public void ExportExcel()
{
if (string.IsNullOrEmpty(_savepath))
{
throw new Exception("保存路径不能为空!");
}
try
{
Microsoft.Office.Interop.Excel.Workbook wb = GetExcelWorkbook();
//保存工作表
wb.SaveCopyAs(_savepath);
}
catch(Exception ex)
{
throw ex;
}
finally
{
Close();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Drawing;
using System.Windows.Forms;
namespace WMS.UI
{
/// <summary>
/// 汪湘明 2012-04-13创建
/// </summary>
public class TableToExcel
{
#region 变量
Microsoft.Office.Interop.Excel.Application xlApp = null;
Microsoft.Office.Interop.Excel.Workbooks wbs = null;
Microsoft.Office.Interop.Excel.Workbook wb = null;
private int _rowindex = 0; //全局行索引(使用时加n行)
private int _saveindex = 0; //保存全局行索引(数据导出后还原全局行索引)
//文本
private string _title = String.Empty; //标题
private string _headerdtext = String.Empty; //页眉,即标题的下一行
private string _footertext = String.Empty; //页脚,即最后一行
//正文(列表)是否显示边框
private bool _isalldisplayborder = true;
//正文(列表)边框类型
private BorderWeightType _borderweight = BorderWeightType.xlThin;
//保存路径
private string _savepath = String.Empty;
//字体
private System.Drawing.Font _titlefont = new System.Drawing.Font("宋体", 15);
private System.Drawing.Font _headerfont = new System.Drawing.Font("宋体", 11);
private System.Drawing.Font _footerfont = new System.Drawing.Font("宋体", 11);
private System.Drawing.Font _bodyheaderfont = new System.Drawing.Font("宋体", 11);
private System.Drawing.Font _bodyfont = new System.Drawing.Font("宋体", 11);
//脚文本Align
private TextAlign _drawfootertextalign = TextAlign.xlHAlignRight;
//要导出的表集合
private List<System.Data.DataTable> _tables = new List<System.Data.DataTable>();
//设置列宽(_isbodydisplayborder为false)
private Dictionary<string, float> _columnswidth = new Dictionary<string, float>();
//设置列的边框,
private Dictionary<string, BorderWeightType> _columnsborder = new Dictionary<string, BorderWeightType>();
//保存Table导入到那个Sheet表(打印时可以判断sheet是否有数据,没有数据则不打印)
private Dictionary<string, System.Data.DataTable> SheetTable = new Dictionary<string, System.Data.DataTable>();
private bool _iswraptext = true; //单元格是否自动换行
private bool _isbodylistheader = true; //是否显示正文列表标题
private bool _isautoconverttext = true; //是否自动转换成文本格式
private bool _isTitleAppendSheetName = false; //标题后面是否追加SheetName
//条码文本
private string _BarCodeText = String.Empty;
//设置打印时页面边距(程序中和Excel中的边距单元不一样(Excel设置0.5大概有5-10px,所以默认5px))
private PaddingF _pageMargin = new PaddingF(5);
private bool _isPrintFooter = true; //是否打印页脚(只跟打印有关)
private string _expandColumnName = String.Empty;
#endregion
#region 构造方法
public TableToExcel(System.Data.DataTable table)
{
_tables = new List<System.Data.DataTable>() { table };
}
public TableToExcel(List<System.Data.DataTable> tables)
{
_tables = tables;
}
public TableToExcel(string savepath, System.Data.DataTable table)
{
_savepath = savepath;
_tables = new List<System.Data.DataTable>() { table };
}
public TableToExcel(string savepath,List<System.Data.DataTable> tables)
{
_savepath = savepath;
_tables = tables;
}
public TableToExcel(string title, string savepath, System.Data.DataTable table)
{
_savepath = savepath;
Title = title;
_tables = new List<System.Data.DataTable>() { table };
}
public TableToExcel(string title, string savepath, List<System.Data.DataTable> tables)
{
_savepath = savepath;
Title = title;
_tables = tables;
}
#endregion