隐藏

C# 多个DataTable导入到Excel多张Sheet表

发布:2014/6/16 10:29:49作者:管理员 来源:本站 浏览次数:1608

这个要引用Office组件的几个DLL文件,条件是先安装Office,而且Office要正确安装才可以

正确安装Office组件:http://blog.csdn.net/wxm3630478/article/details/5888375

 

测试Office版本: Office2007

添加引用 -- Com组件(Tab页) -- Microsoft Excel 12.0 ObjectLibrary    -- 因为要向Excel插入图片,所以用Com组件

 

如果引用 -- .Net组件(Tab页) -- Microsoft.Office.Interop.Excel 版本14.0.0.0 ,则插入图片的方法处会报错,删除即可(如果不需要插入图片)

 

代码可以参考下,这个是我以前写的一个,是按照公司导出到Excel的格式写的,可以抽取一部分用

 

你先把数据分好类,放到不同的DataTable中,然后
TableToExcel类有构造函数需要参数 List<System.Data.DataTable> 集合
这个DataTable集合就会到出到不同的Sheet表,Sheet表的名字就是DataTable的Name属性,
如果Name为空,则是Table1~n

//调用:
List<System.Data.DataTable> dts = new List<System.Data.DataTable>();
DataTable table1 = new DataTable();
table1.Name = "鞋类"
dts.Add(table1);
dts.Add(table2);
dts.Add(table3);
//..........
TableToExcel ex = new TableToExcel("C:\\test.xls",dts)
ex.ExportExcel()

/*----------------------------------------------------------------*/

 

[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Reflection;  
  6. using System.Drawing;  
  7. using System.Windows.Forms;  
  8.   
  9. namespace WMS.UI  
  10. {  
  11.     /// <summary>  
  12.     /// WXM 2012-04-13创建  
  13.     /// </summary>  
  14.     public class TableToExcel  
  15.     {  
  16.         #region 变量  
  17.         Microsoft.Office.Interop.Excel.Application xlApp = null;  
  18.         Microsoft.Office.Interop.Excel.Workbooks wbs = null;  
  19.         Microsoft.Office.Interop.Excel.Workbook wb = null;  
  20.   
  21.         private int _rowindex = 0;   //全局行索引(使用时加n行)  
  22.         private int _saveindex = 0;  //保存全局行索引(数据导出后还原全局行索引)  
  23.         //文本  
  24.         private string  _title = String.Empty;   //标题  
  25.         private string  _headerdtext = String.Empty;   //页眉,即标题的下一行  
  26.         private string _footertext = String.Empty;   //页脚,即最后一行  
  27.   
  28.         //正文(列表)是否显示边框  
  29.         private bool _isalldisplayborder = true;  
  30.         //正文(列表)边框类型  
  31.         private BorderWeightType _borderweight = BorderWeightType.xlThin;  
  32.   
  33.         //保存路径  
  34.         private string _savepath = String.Empty;  
  35.         //字体  
  36.         private System.Drawing.Font _titlefont = new System.Drawing.Font("宋体", 15);  
  37.         private System.Drawing.Font _headerfont = new System.Drawing.Font("宋体", 11);  
  38.         private System.Drawing.Font _footerfont = new System.Drawing.Font("宋体", 11);  
  39.         private System.Drawing.Font _bodyheaderfont = new System.Drawing.Font("宋体", 11);  
  40.         private System.Drawing.Font _bodyfont = new System.Drawing.Font("宋体", 11);  
  41.   
  42.         //脚文本Align  
  43.         private TextAlign _drawfootertextalign = TextAlign.xlHAlignRight;  
  44.   
  45.         //要导出的表集合  
  46.         private List<System.Data.DataTable> _tables = new List<System.Data.DataTable>();  
  47.   
  48.         //设置列宽(_isbodydisplayborder为false)  
  49.         private Dictionary<stringfloat> _columnswidth = new Dictionary<stringfloat>();  
  50.   
  51.         //设置列的边框,  
  52.         private Dictionary<string, BorderWeightType> _columnsborder = new Dictionary<string, BorderWeightType>();  
  53.   
  54.         //保存Table导入到那个Sheet表(打印时可以判断sheet是否有数据,没有数据则不打印)  
  55.         private Dictionary<string, System.Data.DataTable> SheetTable = new Dictionary<string, System.Data.DataTable>();  
  56.   
  57.         private bool _iswraptext = true;  //单元格是否自动换行  
  58.         private bool _isbodylistheader = true;  //是否显示正文列表标题  
  59.         private bool _isautoconverttext = true;  //是否自动转换成文本格式  
  60.         private bool _isTitleAppendSheetName = false;  //标题后面是否追加SheetName  
  61.   
  62.         //条码文本  
  63.         private string _BarCodeText = String.Empty;  
  64.   
  65.         //设置打印时页面边距(程序中和Excel中的边距单元不一样(Excel设置0.5大概有5-10px,所以默认5px))  
  66.         private PaddingF _pageMargin = new PaddingF(5);  
  67.         private bool _isPrintFooter = true;   //是否打印页脚(只跟打印有关)  
  68.         private string _expandColumnName = String.Empty;  
  69.         #endregion  
  70.  
  71.         #region 构造方法  
  72.         public TableToExcel(System.Data.DataTable table)  
  73.         {  
  74.             _tables = new List<System.Data.DataTable>() { table };  
  75.         }  
  76.   
  77.         public TableToExcel(List<System.Data.DataTable> tables)  
  78.         {  
  79.             _tables = tables;  
  80.         }  
  81.   
  82.         public TableToExcel(string savepath, System.Data.DataTable table)  
  83.         {  
  84.             _savepath = savepath;  
  85.             _tables = new List<System.Data.DataTable>() { table };  
  86.         }  
  87.   
  88.         public TableToExcel(string savepath,List<System.Data.DataTable> tables)  
  89.         {  
  90.             _savepath = savepath;  
  91.             _tables = tables;  
  92.         }  
  93.   
  94.         public TableToExcel(string title, string savepath, System.Data.DataTable table)  
  95.         {  
  96.             _savepath = savepath;  
  97.             Title = title;  
  98.             _tables = new List<System.Data.DataTable>() { table };  
  99.         }  
  100.   
  101.         public TableToExcel(string title, string savepath, List<System.Data.DataTable> tables)  
  102.         {  
  103.             _savepath = savepath;  
  104.             Title = title;  
  105.             _tables = tables;  
  106.         }  
  107.         #endregion  
  108.  
  109.         #region 属性  
  110.         /// <summary>  
  111.         /// 行索引(表示从某行开始打印,如0表示从第一行开始)  
  112.         /// </summary>  
  113.         public virtual int RowIndex  
  114.         {  
  115.             get { return _rowindex; }  
  116.             set  
  117.             {  
  118.                 _rowindex = _saveindex = value;  
  119.             }  
  120.         }  
  121.   
  122.         /// <summary>  
  123.         /// 标题  
  124.         /// </summary>  
  125.         public virtual string Title  
  126.         {  
  127.             get { return _title; }  
  128.             set   
  129.             {   
  130.                 _title = value;  
  131.                 IsDrawTitle = !string.IsNullOrEmpty(value);  
  132.             }  
  133.         }  
  134.   
  135.         /// <summary>  
  136.         /// 头文本  
  137.         /// </summary>  
  138.         public virtual string HeaderText  
  139.         {  
  140.             get { return _headerdtext; }  
  141.             set   
  142.             {  
  143.                 _headerdtext = value;  
  144.                 IsDrawHeader = !string.IsNullOrEmpty(value);  
  145.             }  
  146.         }  
  147.   
  148.         /// <summary>  
  149.         /// 脚文本  
  150.         /// </summary>  
  151.         public virtual string FooterText  
  152.         {  
  153.             get { return _footertext; }  
  154.             set   
  155.             {  
  156.                 _footertext = value;  
  157.                 IsDrawFooter = !string.IsNullOrEmpty(value);  
  158.             }  
  159.         }  
  160.   
  161.         /// <summary>  
  162.         /// 保存地址  
  163.         /// </summary>  
  164.         public virtual string SavePath  
  165.         {  
  166.             get { return _savepath; }  
  167.             set   
  168.             {   
  169.                 _savepath = value;  
  170.             }  
  171.         }  
  172.   
  173.         /// <summary>  
  174.         /// 标题字体  
  175.         /// </summary>  
  176.         public virtual System.Drawing.Font TitleFont  
  177.         {  
  178.             get { return _titlefont; }  
  179.             set { _titlefont = value; }  
  180.         }  
  181.   
  182.         /// <summary>  
  183.         /// Header字体  
  184.         /// </summary>  
  185.         public virtual System.Drawing.Font HeaderFont  
  186.         {  
  187.             get { return _headerfont; }  
  188.             set { _headerfont = value; }  
  189.         }  
  190.   
  191.         /// <summary>  
  192.         /// 页脚字体  
  193.         /// </summary>  
  194.         public virtual System.Drawing.Font FooterFont  
  195.         {  
  196.             get { return _footerfont; }  
  197.             set { _footerfont = value; }  
  198.         }  
  199.   
  200.         /// <summary>  
  201.         /// 正文标题字体  
  202.         /// </summary>  
  203.         public virtual System.Drawing.Font BodyHeaderFont  
  204.         {  
  205.             get { return _bodyheaderfont; }  
  206.             set { _bodyheaderfont = value; }  
  207.         }  
  208.   
  209.         /// <summary>  
  210.         /// 正文字体  
  211.         /// </summary>  
  212.         public virtual System.Drawing.Font BodyFont  
  213.         {  
  214.             get { return _bodyfont; }  
  215.             set { _bodyfont = value; }  
  216.         }  
  217.   
  218.         /// <summary>  
  219.         /// 导出表集合  
  220.         /// </summary>  
  221.         public virtual List<System.Data.DataTable> Tables  
  222.         {  
  223.             get { return _tables; }  
  224.             set { _tables = value; }  
  225.         }  
  226.   
  227.         /// <summary>  
  228.         /// 列宽集合(A:A 表示第一列,A:B表示第一二列)  
  229.         /// </summary>  
  230.         public virtual Dictionary<stringfloat> ColumnsWidth  
  231.         {  
  232.             get { return _columnswidth; }  
  233.             set { _columnswidth = value; }  
  234.         }  
  235.   
  236.         /// <summary>  
  237.         /// 那些列显示边框  
  238.         /// </summary>  
  239.         public virtual Dictionary<string, BorderWeightType> ColumnsBorder  
  240.         {  
  241.             get { return _columnsborder; }  
  242.             set { _columnsborder = value; }  
  243.         }  
  244.   
  245.         public virtual bool IsDrawTitle { getset; }  
  246.   
  247.         public virtual bool IsDrawHeader { getset; }  
  248.   
  249.         public virtual bool IsDrawFooter { getset; }  
  250.   
  251.         public virtual TextAlign DrawFooterTextAlign  
  252.         {  
  253.             get { return _drawfootertextalign; }  
  254.             set { _drawfootertextalign = value; }  
  255.         }  
  256.   
  257.         /// <summary>  
  258.         /// 是否显示数据列表标题  
  259.         /// </summary>  
  260.         public virtual bool IsBodyListHeader   
  261.         {  
  262.             get { return _isbodylistheader; }  
  263.             set { _isbodylistheader = value; }  
  264.         }  
  265.   
  266.         /// <summary>  
  267.         /// 正文(列表)是否显示边框  
  268.         /// </summary>  
  269.         public virtual bool IsDispalyBorderAll  
  270.         {  
  271.             get { return _isalldisplayborder; }  
  272.             set { _isalldisplayborder = value; }  
  273.         }  
  274.   
  275.         /// <summary>  
  276.         /// 是否自动转换成文本格式  
  277.         /// </summary>  
  278.         public virtual bool IsAutoConvertText  
  279.         {  
  280.             get { return _isautoconverttext; }  
  281.             set  
  282.             {  
  283.                 _isautoconverttext = value;  
  284.             }  
  285.         }  
  286.   
  287.         /// <summary>  
  288.         /// 标题后面是否追加SheetName  
  289.         /// </summary>  
  290.         public virtual bool IsTitleAppendSheetName  
  291.         {  
  292.             get { return _isTitleAppendSheetName; }  
  293.             set  
  294.             {  
  295.                 _isTitleAppendSheetName = value;  
  296.             }  
  297.         }  
  298.   
  299.         /// <summary>  
  300.         /// 正文(列表)边框类型  
  301.         /// </summary>  
  302.         public virtual BorderWeightType BorderWeight  
  303.         {  
  304.             get { return _borderweight; }  
  305.             set { _borderweight = value; }  
  306.         }  
  307.   
  308.         /// <summary>  
  309.         /// 单元格是否自动适应宽度  
  310.         /// </summary>  
  311.         public virtual bool IsAutoFit { getset; }  
  312.   
  313.         /// <summary>  
  314.         /// 单元格是否自动换行  
  315.         /// </summary>  
  316.         public virtual bool IsWrapText   
  317.         {  
  318.             get { return _iswraptext; }  
  319.             set { _iswraptext = value; }  
  320.         }  
  321.   
  322.         /// <summary>  
  323.         /// 条码文本(长度不能超过8位,超过8位请更新图片模版,否则打印出界)  
  324.         /// </summary>  
  325.         public string BarCodeText  
  326.         {  
  327.             get { return _BarCodeText; }  
  328.             set   
  329.             {  
  330.                 _BarCodeText = value;  
  331.                 if (!String.IsNullOrEmpty(value))  
  332.                 {  
  333.                     IsDrawTitle = true;  
  334.                 }  
  335.                 else  
  336.                 {  
  337.                     IsDrawTitle = !string.IsNullOrEmpty(_title);  
  338.                 }  
  339.             }  
  340.         }  
  341.   
  342.         /// <summary>  
  343.         /// 设置打印时页面边距(程序中和Excel中的边距单元不一样(Excel设置0.5大概有10px,所以默认10px))  
  344.         /// </summary>  
  345.         public PaddingF PageMargin  
  346.         {  
  347.             get { return _pageMargin; }  
  348.             set  
  349.             {  
  350.                 _pageMargin = value;  
  351.             }  
  352.         }  
  353.   
  354.         /// <summary>  
  355.         /// 是否打印页脚(只跟打印有关)  
  356.         /// </summary>  
  357.         public bool IsPrintFooter  
  358.         {  
  359.             get { return _isPrintFooter; }  
  360.             set  
  361.             {  
  362.                 _isPrintFooter = value;  
  363.             }  
  364.         }  
  365.   
  366.         /// <summary>  
  367.         /// 要扩大的列(打印时,如果设置了列宽自动适应,有可能总列宽缩小或放大,所以缩小或放大的宽度要算在ExpandColumnName列上)  
  368.         /// 格式 A:A  B:B  C:C  
  369.         /// </summary>  
  370.         public string ExpandColumnName  
  371.         {  
  372.             get { return _expandColumnName; }  
  373.             set { _expandColumnName = value; }  
  374.         }  
  375.         #endregion  
  376.   
  377.         public void ExportExcel()  
  378.         {  
  379.             if (string.IsNullOrEmpty(_savepath))  
  380.             {  
  381.                 throw new Exception("保存路径不能为空!");  
  382.             }  
  383.   
  384.             try  
  385.             {  
  386.                 Microsoft.Office.Interop.Excel.Workbook wb = GetExcelWorkbook();  
  387.                 //保存工作表  
  388.                 wb.SaveCopyAs(_savepath);  
  389.             }  
  390.             catch(Exception ex)  
  391.             {  
  392.                 throw ex;  
  393.             }  
  394.             finally  
  395.             {  
  396.                 Close();   
  397.             }  
  398.         }  
  399.   
  400.         /// <summary>  
  401.         /// 打印Excel (打印设置列宽不能出现E:F)  
  402.         /// </summary>  
  403.         /// <param name="isLandscape">是否所有Sheet横向打印(true为横向打印)</param>  
  404.         public void PrintExcel(bool isLandscape)  
  405.         {  
  406.             try  
  407.             {  
  408.                 Microsoft.Office.Interop.Excel.Workbook wb = GetExcelWorkbook();  
  409.                 if (wb == null)  
  410.                 {  
  411.                     return;  
  412.                 }  
  413.   
  414.                 foreach (var v in wb.Worksheets)  
  415.                 {  
  416.                     try  
  417.                     {  
  418.                         Microsoft.Office.Interop.Excel.Worksheet wsheet = ((Microsoft.Office.Interop.Excel.Worksheet)v);  
  419.                         //无数据表无需打印  
  420.                         if (SheetTable[wsheet.Name].Rows.Count > 0)  
  421.                         {  
  422.                             if (isLandscape)  
  423.                             {  
  424.                                 //设置横向打印,默认纵向  
  425.                                 wsheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;   //横向打印  
  426.                             }  
  427.                             //设置纸张类型  
  428.                             wsheet.PageSetup.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperA4;  
  429.                             //设置打印边距  
  430.                             wsheet.PageSetup.TopMargin = _pageMargin.Top;  
  431.                             wsheet.PageSetup.BottomMargin = _isPrintFooter ? 12.00 : _pageMargin.Bottom;  
  432.                             wsheet.PageSetup.LeftMargin = _pageMargin.Left;  
  433.                             wsheet.PageSetup.RightMargin = _pageMargin.Right;  
  434.                             //wsheet.PageSetup.CenterHorizontally = true;  
  435.                             //wsheet.PageSetup.CenterVertically = true;   //这2句是居中打印  
  436.                             //wsheet.PageSetup.CenterFooter = "&\"Arial\"&12 " + obj.pageid.ToString();//页脚字体和大小  
  437.                             wsheet.PageSetup.FooterMargin = 1;  
  438.                             if (_isPrintFooter)  
  439.                             {  
  440.                                 //打印页脚  
  441.                                 wsheet.PageSetup.LeftFooter = wsheet.Name;  
  442.                                 wsheet.PageSetup.CenterFooter = @"第 &P 页/共 &N 页";  
  443.                                 wsheet.PageSetup.RightFooter = "打印时间:" + DateTime.Now.ToString();  
  444.                             }  
  445.                             //列宽自适应 (中英文数字混排的时候,如果不设置自动适应列宽的话,有的时候打印会缺字)  
  446.                             wsheet.Columns.AutoFit();  
  447.                             if (!String.IsNullOrEmpty(_expandColumnName))  //如果有扩大的列  
  448.                             {  
  449.                                 float swidth = 0;   //目前实际宽度  
  450.                                 float ywidth = 0;   //应该显示的宽度  
  451.                                 foreach (var vk in _columnswidth)  
  452.                                 {  
  453.                                     object obj = ((Microsoft.Office.Interop.Excel.Range)wsheet.Columns[vk.Key, Type.Missing]).ColumnWidth;  
  454.                                     swidth += float.Parse(obj.ToString());  
  455.                                     ywidth += vk.Value;  //设置的列宽总和  
  456.                                 }  
  457.                                 //自适应宽度后,总宽度小于我打印时设置的总宽度,则把缩小的宽度添加到可以扩大的列上  
  458.                                 //扩大某列的宽度  
  459.                                 Microsoft.Office.Interop.Excel.Range colrange = (Microsoft.Office.Interop.Excel.Range)wsheet.Columns[_expandColumnName, Type.Missing];  
  460.                                 float cwidth = float.Parse(colrange.ColumnWidth.ToString());  //列当前宽度  
  461.                                 if (swidth < ywidth)  
  462.                                 {  
  463.                                     colrange.ColumnWidth = (cwidth + (ywidth - swidth));  
  464.                                 }  
  465.                                 else  
  466.                                 {  
  467.                                     colrange.ColumnWidth = (cwidth - (swidth - ywidth));  
  468.                                 }  
  469.                             }  
  470.                             //自适应行  
  471.                             wsheet.Rows.AutoFit();  
  472.                             //打印  
  473.                             wsheet.PrintOutEx();  
  474.                         }  
  475.                     }  
  476.                     catch  
  477.                     { }  
  478.                 }  
  479.                 //打印整个Workbook(所有Sheet表都打印出来)  
  480.                 //wb.PrintOutEx();  
  481.             }  
  482.             catch (Exception ex)  
  483.             {  
  484.                 throw ex;  
  485.             }  
  486.             finally  
  487.             {  
  488.                 Close();  
  489.             }  
  490.         }  
  491.   
  492.         /// <summary>  
  493.         /// 打印Excel 默认纵向打印  
  494.         /// </summary>  
  495.         public void PrintExcel()  
  496.         {  
  497.             PrintExcel(false);  
  498.         }  
  499.   
  500.         /// <summary>  
  501.         /// 获取Workbook  
  502.         /// </summary>  
  503.         /// <param name="table"></param>  
  504.         /// <param name="filename"></param>  
  505.         public Microsoft.Office.Interop.Excel.Workbook GetExcelWorkbook()  
  506.         {  
  507.             if (_tables.Count == 0)  
  508.             {  
  509.                 throw new Exception("Tables集合必须大于零!");  
  510.             }  
  511.   
  512.             if (_rowindex < 0)  
  513.             {  
  514.                 _rowindex = 0;  
  515.             }  
  516.             xlApp = new Microsoft.Office.Interop.Excel.Application();  
  517.             xlApp.Visible = false;  
  518.             xlApp.DisplayAlerts = false;  
  519.             wbs = xlApp.Workbooks;  
  520.             wb = wbs.Add(Missing.Value);   //添加一个工作簿  
  521.             //添加Sheet表,新建一个Excel文件时候,一般会默认有3个Sheet表,所以用[table.Count - wb.Sheets.Count]  
  522.             int tabcount = _tables.Count;  
  523.             int sheets = wb.Worksheets.Count;  //获取默认Sheet表个数,一般默认3个  
  524.             if (tabcount > sheets)  
  525.             {  
  526.                 wb.Worksheets.Add(Missing.Value, Missing.Value, tabcount - sheets, Missing.Value);  
  527.             }  
  528.             //删除多余Sheet表  
  529.             //((Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[index]).Delete();  
  530.             //写入Excel  
  531.             WriteExcelSheet(wb);  
  532.             //保存工作表  
  533.             //xlApp.ActiveWorkbook.SaveCopyAs(filename);  
  534.             //wb.SaveCopyAs(_savepath);  
  535.             return wb;  
  536.         }  
  537.   
  538.         private void Close()  
  539.         {  
  540.             #region 关闭Excel进程  
  541.             if (wb != null)  
  542.             {  
  543.                 wb.Close(false, _savepath, Missing.Value);  
  544.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);  
  545.                 wb = null;  
  546.             }  
  547.             if (wbs != null)  
  548.             {  
  549.                 wbs.Close();  
  550.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs);  
  551.                 wbs = null;  
  552.             }  
  553.             if (xlApp != null)  
  554.             {  
  555.                 xlApp.Quit();  
  556.                 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);  
  557.                 xlApp = null;  
  558.             }  
  559.             GC.Collect();  
  560.             GC.WaitForPendingFinalizers();  
  561.             #endregion  
  562.         }  
  563.   
  564.         /// <summary>  
  565.         /// 写入Excel的Sheet表  
  566.         /// </summary>  
  567.         /// <param name="wb"></param>  
  568.         /// <param name="tables"></param>  
  569.         private void WriteExcelSheet(Microsoft.Office.Interop.Excel.Workbook wb)  
  570.         {  
  571.             List<string> SheetNames = GetSheetsName(wb);  //Sheet表名字集合,防止相同名称,出现错误!  
  572.             for (int i = 0; i < _tables.Count; i++)  
  573.             {  
  574.                 System.Data.DataTable table = _tables[i];  
  575.                 int rows = table.Rows.Count;  
  576.                 int cols = table.Columns.Count;  
  577.                 //获取Sheet表  
  578.                 Microsoft.Office.Interop.Excel.Worksheet wsheet = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[i + 1];   //wb.Worksheets 所以从1开始  
  579.                 //选中表  
  580.                 wsheet.Select();  
  581.                 //设置表名字  
  582.                 wsheet.Name = GetCorrectSheetName(SheetNames, table.TableName, (i + 1)); //Name要特别注意  
  583.   
  584.                 //保存DataTable和Sheet表的绑定  
  585.                 SheetTable.Add(wsheet.Name, table);  
  586.   
  587.                 //设置总标题  
  588.                 DrawTitle(wsheet, cols);  
  589.                 //设置HeaderText  
  590.                 if (IsDrawHeader)  
  591.                 {  
  592.                     DrawText(wsheet, _headerdtext, _headerfont, TextAlign.xlHAlignLeft, cols);// Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft, cols);  
  593.                 }  
  594.                 //数据列表标题  
  595.                 //Microsoft.Office.Interop.Excel.Range r = ret.get_Range(ret.Cells[1, 1], ret.Cells[1, table.Columns.Count]);  
  596.                 if (_isbodylistheader)  
  597.                 {  
  598.                     _rowindex++;  
  599.                     Microsoft.Office.Interop.Excel.Range range = wsheet.Range[wsheet.Cells[_rowindex, 1], wsheet.Cells[_rowindex, cols]];  
  600.                     object[] header = new object[cols];  
  601.                     for (int j = 0; j < cols; j++)  
  602.                     {  
  603.                         header[j] = table.Columns[j].ToString();  
  604.                     }  
  605.                     range.Value2 = header;  
  606.                     SetFont(range, _bodyheaderfont);  
  607.                     if (_isalldisplayborder)  
  608.                     {  
  609.                         range.Borders.LineStyle = 1;  //边框线  
  610.                         range.Borders.Weight = (int)_borderweight;  
  611.                     }  
  612.                 }  
  613.   
  614.                 if (rows > 0)  
  615.                 {  
  616.                     _rowindex++;  
  617.                     Microsoft.Office.Interop.Excel.Range range = wsheet.get_Range("A" + _rowindex, Missing.Value);  
  618.                     object[,] objData = new Object[rows, cols];  
  619.                     for (int row = 0; row < rows; row++)  
  620.                     {  
  621.                         for (int col = 0; col < cols; col++)  
  622.                         {  
  623.                             string converttxt = String.Empty;  
  624.                             if (_isautoconverttext)  
  625.                             {  
  626.                                 converttxt = "'";  
  627.                             }  
  628.                             objData[row, col] = converttxt + table.Rows[row][col].ToString();  //随后数据后面加单引号  
  629.                         }  
  630.                     }  
  631.                     range = range.get_Resize(rows, cols);  
  632.                     range.Value2 = objData;  
  633.                     SetFont(range, _bodyfont);  
  634.                     if (_isalldisplayborder)  
  635.                     {  
  636.                         range.Borders.LineStyle = 1;  //加线框  
  637.                         range.Borders.Weight = (int)_borderweight;  
  638.                     }  
  639.                     else  
  640.                     {  
  641.                         //((Microsoft.Office.Interop.Excel.Range)range.Columns["A:A", Type.Missing]).Borders.LineStyle = 1;  
  642.                         //((Microsoft.Office.Interop.Excel.Range)range.Columns["A:A", Type.Missing]).Borders.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium;  
  643.                         //指定的列显示边框  
  644.                         foreach (var vk in _columnsborder)  
  645.                         {   
  646.                             Microsoft.Office.Interop.Excel.Borders borders = ((Microsoft.Office.Interop.Excel.Range)range.Columns[vk.Key, Type.Missing]).Borders;  
  647.                             borders.LineStyle = 1;  
  648.                             borders.Weight = (int)vk.Value;  
  649.                         }  
  650.                     }  
  651.                     if (IsAutoFit)  
  652.                     {  
  653.                         range.EntireColumn.AutoFit();  //自动适应列  
  654.                     }  
  655.                     //更新行索引  
  656.                     _rowindex += (rows - 1);  
  657.                 }  
  658.   
  659.                 //设置FooterText  
  660.                 if (IsDrawFooter)  
  661.                 {  
  662.                     if (rows == 0)  
  663.                     {  
  664.                         _rowindex += 2; //与Body隔一行  
  665.                     }  
  666.                     DrawText(wsheet, _footertext, _footerfont,DrawFooterTextAlign, cols);  
  667.                 }  
  668.                 //所有列自动换行  
  669.                 wsheet.Columns.WrapText = _iswraptext;  
  670.                 //设置列宽  
  671.                 if (!IsAutoFit)  
  672.                 {  
  673.                     foreach (var vk in _columnswidth)  
  674.                     {  
  675.                         ((Microsoft.Office.Interop.Excel.Range)wsheet.Columns[vk.Key, System.Type.Missing]).ColumnWidth = vk.Value;  
  676.                     }    
  677.                 }  
  678.                 //保存名字  
  679.                 SheetNames.Add(wsheet.Name);  
  680.                 //还原  
  681.                 _rowindex = _saveindex;  
  682.             }  
  683.             //选中第一个表  
  684.             ((Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1]).Select();    
  685.         }  
  686.   
  687.         /// <summary>  
  688.         /// 写标题  
  689.         /// </summary>  
  690.         /// <param name="wsheet"></param>  
  691.         /// <param name="title"></param>  
  692.         /// <param name="colcount"></param>  
  693.         private void DrawTitle(Microsoft.Office.Interop.Excel.Worksheet wsheet, int colcount)  
  694.         {  
  695.             if (!IsDrawTitle)  
  696.             {  
  697.                 return;  
  698.             }  
  699.             _rowindex++;  //加行数  
  700.   
  701.             Microsoft.Office.Interop.Excel.Range cellrange = (Microsoft.Office.Interop.Excel.Range)wsheet.Cells[_rowindex, 1];  
  702.             //cellrange.RowHeight = 31;  //行高31  
  703.             //插入图片  
  704.             InsertImage(GetImagePath(), cellrange, wsheet);  
  705.   
  706.             //取得整个报表的标题  
  707.             string titletxt = _title;  
  708.             if (_isTitleAppendSheetName)  
  709.             {  
  710.                 titletxt += "(" + wsheet.Name + ")";  
  711.             }  
  712.             wsheet.Cells[_rowindex, 1] = titletxt;  
  713.             //设置整个报表的标题格式  
  714.             Microsoft.Office.Interop.Excel.Range range = wsheet.Range[wsheet.Cells[_rowindex, 1], wsheet.Cells[_rowindex, colcount]];  
  715.             //设置字体  
  716.             SetFont(range, _titlefont);  
  717.             //range.Borders.LineStyle = 1;  //边线框  
  718.             //下边框线  
  719.             Microsoft.Office.Interop.Excel.Border border = range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom);  
  720.             //border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlSlantDashDot;// 选择先的类型  
  721.             border.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium;  
  722.             //字体剧中  
  723.             range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenterAcrossSelection;  
  724.             //合并单元格  
  725.             range.Merge(range.MergeCells);  
  726.             range.WrapText = true;  
  727.         }  
  728.   
  729.         /// <summary>  
  730.         /// 写文本  
  731.         /// </summary>  
  732.         /// <param name="wsheet"></param>  
  733.         /// <param name="title"></param>  
  734.         /// <param name="colcount"></param>  
  735.         private void DrawText(Microsoft.Office.Interop.Excel.Worksheet wsheet, string txt,System.Drawing.Font font,TextAlign align, int colcount)  
  736.         {  
  737.             _rowindex++;  
  738.             //取得整个报表的标题  
  739.             wsheet.Cells[_rowindex, 1] = txt;  
  740.             //设置整个报表的标题格式  
  741.             Microsoft.Office.Interop.Excel.Range range = wsheet.Range[wsheet.Cells[_rowindex, 1], wsheet.Cells[_rowindex, colcount]];  
  742.             SetFont(range, font);  
  743.             //range.Borders.LineStyle = 1;  //边线  
  744.             range.HorizontalAlignment = (int)align; // Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;  
  745.             //合并单元格  
  746.             range.Merge(range.MergeCells);  
  747.         }  
  748.   
  749.         /// <summary>  
  750.         /// 插入图片方法  
  751.         /// </summary>  
  752.         /// <param name="imgpath"></param>  
  753.         /// <param name="range"></param>  
  754.         /// <param name="wsheet"></param>  
  755.         private void InsertImage(string imgpath, Microsoft.Office.Interop.Excel.Range range, Microsoft.Office.Interop.Excel.Worksheet wsheet)  
  756.         {  
  757.             if (System.String.IsNullOrEmpty(imgpath))  
  758.             {  
  759.                 return;  
  760.             }  
  761.             try  
  762.             {  
  763.                 //126  
  764.                 range.Select();  
  765.                 float PicLeft, PicTop;  
  766.                 PicLeft = Convert.ToSingle(range.Left);  
  767.                 PicTop = Convert.ToSingle(range.Top) + 1.5F;  
  768.                 wsheet.Shapes.AddPicture(imgpath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, PicLeft, PicTop, -1, -1);  //-1表示用源图尺寸  
  769.             }  
  770.             catch { }  
  771.         }  
  772.   
  773.         /// <summary>  
  774.         /// 设置字体  
  775.         /// </summary>  
  776.         /// <param name="range"></param>  
  777.         /// <param name="font"></param>  
  778.         private void SetFont(Microsoft.Office.Interop.Excel.Range range, System.Drawing.Font font)  
  779.         {  
  780.             range.Font.Bold = font.Bold;  
  781.             range.Font.Italic = font.Italic;  
  782.             range.Font.Name = font.Name;  
  783.             range.Font.Size = font.Size;  
  784.             range.Font.Underline = font.Underline;  
  785.         }  
  786.   
  787.         /// <summary>  
  788.         /// 获取Workbook中所有Sheet表的名称  
  789.         /// </summary>  
  790.         /// <param name="wb"></param>  
  791.         /// <returns></returns>  
  792.         private List<string> GetSheetsName(Microsoft.Office.Interop.Excel.Workbook wb)  
  793.         {  
  794.             List<string> names = new List<string>();  
  795.             foreach (var sheet in wb.Worksheets)  
  796.             {  
  797.                 string name = ((Microsoft.Office.Interop.Excel.Worksheet)sheet).Name;  
  798.                 if (!String.IsNullOrEmpty(name))  
  799.                 {  
  800.                     names.Add(name);  
  801.                 }  
  802.             }  
  803.             return names;  
  804.         }  
  805.   
  806.         /// <summary>  
  807.         /// 获取一个合法Sheet名称  
  808.         /// </summary>  
  809.         /// <returns></returns>  
  810.         private string GetCorrectSheetName(List<string> existnames, string tabname, int tabindex)  
  811.         {  
  812.             string name = String.Empty;  
  813.             //是否为空,是否大于31,是否有特殊字符  
  814.             //验证名字是否合法  
  815.             if (!VerifySheetName(tabname))  
  816.             {  
  817.                 tabname = "Table" + tabindex;  
  818.             }  
  819.   
  820.             bool isexist = existnames.Exists(n => n.Equals(tabname));  //是否存在  
  821.             if (isexist)  
  822.             {  
  823.                 tabindex++;  
  824.                 name = GetCorrectSheetName(existnames, "Table" + tabindex, tabindex);  
  825.             }  
  826.             else  
  827.             {  
  828.                 name = tabname;  
  829.             }  
  830.             return name;  
  831.         }  
  832.   
  833.         /// <summary>  
  834.         /// 判断是否合法  
  835.         /// </summary>  
  836.         private bool VerifySheetName(string sheetname)  
  837.         {  
  838.             if (string.IsNullOrEmpty(sheetname) || sheetname.Length > 31)  
  839.             {  
  840.                 return false;  
  841.             }  
  842.   
  843.             if (sheetname.Contains("\\") || sheetname.Contains("/"))  
  844.             {  
  845.                 return false;  
  846.             }  
  847.   
  848.             if (sheetname.Contains(":") || sheetname.Contains(":"))  
  849.             {  
  850.                 return false;  
  851.             }  
  852.   
  853.             if (sheetname.Contains("?") || sheetname.Contains("?"))  
  854.             {  
  855.                 return false;  
  856.             }  
  857.   
  858.             if (sheetname.Contains("[") || sheetname.Contains("]"))  
  859.             {  
  860.                 return false;  
  861.             }  
  862.             return true;  
  863.         }  
  864.   
  865.         /// <summary>  
  866.         /// 保存图片到本地,并返回图片地址  
  867.         /// </summary>  
  868.         /// <returns></returns>  
  869.         public string GetImagePath()  
  870.         {  
  871.             if (String.IsNullOrEmpty(BarCodeText))  
  872.             {  
  873.                 return String.Empty;  
  874.             }  
  875.             string imgpath = String.Empty;  
  876.             Bitmap bitmap = null;  
  877.             Graphics gs = null;  
  878.             try  
  879.             {  
  880.                 bitmap = WMS.UI.Properties.Resources.BarCode_Template;  
  881.                 gs = Graphics.FromImage(bitmap);   //获取模版图片的Graphics对象  
  882.                 BarCode39 barcode = new BarCode39();  
  883.                 barcode.BarcodeText = BarCodeText; //分拣单编号  
  884.                 barcode.DrawBarBit(gs, new Rectangle(0, 0, bitmap.Width, bitmap.Height));  //条码画到模版图片上  
  885.                 imgpath = Application.StartupPath + "\\BarCode.jpg";  
  886.                 bitmap.Save(imgpath);   //保存图片  
  887.             }  
  888.             catch  
  889.             {  
  890.                 imgpath = "";  
  891.             }  
  892.             finally  
  893.             {  
  894.                 if (gs != null)  
  895.                 {  
  896.                     gs.Dispose();  
  897.                     gs = null;  
  898.                 }  
  899.                 if (bitmap != null)  
  900.                 {  
  901.                     bitmap.Dispose();  
  902.                     bitmap = null;  
  903.                 }  
  904.             }  
  905.             return imgpath;  
  906.         }  
  907.     }  
  908.   
  909.     public enum BorderWeightType  
  910.     {  
  911.         /// <summary>  
  912.         /// 中  
  913.         /// </summary>  
  914.         xlMedium = -4138,  
  915.         /// <summary>  
  916.         /// 极细  
  917.         /// </summary>  
  918.         xlHairline = 1,  
  919.         /// <summary>  
  920.         /// 细  
  921.         /// </summary>  
  922.         xlThin = 2,  
  923.         /// <summary>  
  924.         /// 粗  
  925.         /// </summary>  
  926.         xlThick = 4,  
  927.     }  
  928.   
  929.     public enum BorderLineStyle  
  930.     {  
  931.         /// <summary>  
  932.         /// 无  
  933.         /// </summary>  
  934.         xlLineStyleNone = -4142,  
  935.         /// <summary>  
  936.         /// 双划线  
  937.         /// </summary>  
  938.         xlDouble = -4119,  
  939.         /// <summary>  
  940.         /// 点线  
  941.         /// </summary>  
  942.         xlDot = -4118,  
  943.         /// <summary>  
  944.         /// 划线  
  945.         /// </summary>  
  946.         xlDash = -4115,  
  947.         /// <summary>  
  948.         /// 连续线  
  949.         /// </summary>  
  950.         xlContinuous = 1,  
  951.         /// <summary>  
  952.         /// 点划线  
  953.         /// </summary>  
  954.         xlDashDot = 4,  
  955.         /// <summary>  
  956.         /// 双点的划线  
  957.         /// </summary>  
  958.         xlDashDotDot = 5,  
  959.         /// <summary>  
  960.         /// 倾斜点划线  
  961.         /// </summary>  
  962.         xlSlantDashDot = 13,  
  963.     }  
  964.   
  965.     public enum TextAlign  
  966.     {  
  967.         xlHAlignRight = -4152,  
  968.         xlHAlignLeft = -4131,  
  969.         xlHAlignJustify = -4130,  
  970.         xlHAlignDistributed = -4117,  
  971.         xlHAlignCenter = -4108,  
  972.         xlHAlignGeneral = 1,  
  973.         xlHAlignFill = 5,  
  974.         xlHAlignCenterAcrossSelection = 7,  
  975.     }  
  976.   
  977.     public struct PaddingF  
  978.     {  
  979.         double _Left;  
  980.         double _Right;  
  981.         double _Top;  
  982.         double _Bottom;  
  983.   
  984.         public PaddingF(double all)  
  985.         {  
  986.             this._Left = all;  
  987.             this._Right = all;  
  988.             this._Top = all;  
  989.             this._Bottom = all;  
  990.         }  
  991.   
  992.         public PaddingF(double left, double top, double right, double bottom)  
  993.         {  
  994.             this._Left = left;  
  995.             this._Right = right;  
  996.             this._Top = top;  
  997.             this._Bottom = bottom;  
  998.         }  
  999.   
  1000.         public void SetValue(double all)  
  1001.         {  
  1002.             SetValue(all, all, all, all);  
  1003.         }  
  1004.   
  1005.         public void SetValue(double top, double left, double right, double bottom)  
  1006.         {  
  1007.             this._Left = left;  
  1008.             this._Right = right;  
  1009.             this._Top = top;  
  1010.             this._Bottom = bottom;  
  1011.         }  
  1012.   
  1013.         public double Left  
  1014.         {  
  1015.             get { return _Left; }  
  1016.             set  
  1017.             {  
  1018.                 _Left = value;  
  1019.             }  
  1020.         }  
  1021.   
  1022.         public double Right  
  1023.         {  
  1024.             get { return _Right; }  
  1025.             set  
  1026.             {  
  1027.                 _Right = value;  
  1028.             }  
  1029.         }  
  1030.   
  1031.         public double Top  
  1032.         {  
  1033.             get { return _Top; }  
  1034.             set  
  1035.             {  
  1036.                 _Top = value;  
  1037.             }  
  1038.         }  
  1039.   
  1040.         public double Bottom  
  1041.         {  
  1042.             get { return _Bottom; }  
  1043.             set  
  1044.             {  
  1045.                 _Bottom = value;  
  1046.             }  
  1047.         }  
  1048.     }  
  1049.  
  1050.     #region BarCode 条形码  
  1051.     public class BarCode39  
  1052.     {  
  1053.         public string BarcodeText = string.Empty;  
  1054.   
  1055.         public int BarcodeHeight = 0;  
  1056.         public int BarcodeWidth = 0;  
  1057.   
  1058.         public Font footerFont = new Font("微软雅黑", 13.0f);   /*Bob 修改了字体大小*/  
  1059.   
  1060.         private double wideToNarrowRatio = 3.0;  
  1061.   
  1062.         public double WideToNarrowRatio  
  1063.         {  
  1064.             get { return wideToNarrowRatio; }  
  1065.             set { wideToNarrowRatio = value; }  
  1066.         }  
  1067.         private int weight = 1;  
  1068.   
  1069.         public int Weight  
  1070.         {  
  1071.             get { return weight; }  
  1072.             set { weight = value; }  
  1073.         }  
  1074.         /// <summary>  
  1075.         /// 39条码中能使用的字符  
  1076.         /// </summary>  
  1077.         private String alphabet39 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*";  
  1078.  
  1079.         #region  
  1080.         private String[] coded39Char =   
  1081.         {  
  1082.             /* 0 */ "001100100",   
  1083.             /* 1 */ "100010100",   
  1084.             /* 2 */ "010010100",   
  1085.             /* 3 */ "110000100",  
  1086.             /* 4 */ "001010100",   
  1087.             /* 5 */ "101000100",   
  1088.             /* 6 */ "011000100",   
  1089.             /* 7 */ "000110100",  
  1090.             /* 8 */ "100100100",   
  1091.             /* 9 */ "010100100",   
  1092.             /* A */ "100010010",   
  1093.             /* B */ "010010010",  
  1094.             /* C */ "110000010",   
  1095.             /* D */ "001010010",   
  1096.             /* E */ "101000010",   
  1097.             /* F */ "011000010",  
  1098.             /* G */ "000110010",   
  1099.             /* H */ "100100010",   
  1100.             /* I */ "010100010",   
  1101.             /* J */ "001100010",  
  1102.             /* K */ "100010001",   
  1103.             /* L */ "010010001",   
  1104.             /* M */ "110000001",   
  1105.             /* N */ "001010001",  
  1106.             /* O */ "101000001",   
  1107.             /* P */ "011000001",   
  1108.             /* Q */ "000110001",   
  1109.             /* R */ "100100001",  
  1110.             /* S */ "010100001",   
  1111.             /* T */ "001100001",   
  1112.             /* U */ "100011000",   
  1113.             /* V */ "010011000",  
  1114.             /* W */ "110001000",   
  1115.             /* X */ "001011000",   
  1116.             /* Y */ "101001000",   
  1117.             /* Z */ "011001000",  
  1118.             /* - */ "000111000",   
  1119.             /* . */ "110000100",   
  1120.             /*' '*/ "011000100",  
  1121.             /* $ */ "010101000",  
  1122.             /* / */ "010100010",   
  1123.             /* + */ "010001010",   
  1124.             /* % */ "100101000",   
  1125.             /* * */ "001101000"   
  1126.         };  
  1127.         #endregion  
  1128.   
  1129.         public BarCode39()  
  1130.         {  
  1131.             BarcodeText = "1234";  
  1132.         }  
  1133.         /// <summary>  
  1134.         /// 为了使得条形码居中先要计算条形码的Left和Top坐标  
  1135.         /// </summary>  
  1136.         /// <returns></returns>  
  1137.         private int getX()  
  1138.         {  
  1139.             int currentLocation = 0;  
  1140.             string strBarcode = "*" + BarcodeText.ToUpper() + "*";  
  1141.             for (int i = 0; i < strBarcode.Length; i++)  
  1142.             {  
  1143.                 string encodedString = coded39Char[alphabet39.IndexOf(strBarcode[i])];  
  1144.   
  1145.                 for (int j = 0; j < 5; j++)  
  1146.                 {  
  1147.   
  1148.                     if (encodedString[j] == '0')  
  1149.                     {  
  1150.                         currentLocation += weight;  
  1151.                     }  
  1152.                     else  
  1153.                     {  
  1154.                         currentLocation += 3 * weight;  
  1155.                     }  
  1156.                     //画第6个     5   白条   
  1157.                     if ((j + 5) < 9)  
  1158.                     {  
  1159.                         if (encodedString[j + 5] == '0')  
  1160.                         {  
  1161.                             currentLocation += weight;  
  1162.                         }  
  1163.                         else  
  1164.                         {  
  1165.                             currentLocation += 3 * weight;  
  1166.                         }  
  1167.                     }  
  1168.                 }  
  1169.                 currentLocation += weight;  
  1170.             }  
  1171.             return currentLocation;  
  1172.         }  
  1173.         /// <summary>  
  1174.         /// 显示条形码  
  1175.         /// </summary>  
  1176.         /// <param name="g">GDI+</param>  
  1177.         /// <param name="rects">画图区域</param>  
  1178.         protected void DrawBitmap(Graphics g, Rectangle rects)  
  1179.         {  
  1180.             if (BarcodeText == ""return;  
  1181.             string strBarcode = "*" + BarcodeText.ToUpper() + "*";  
  1182.             //string strBarcode =  BarcodeText.ToUpper() ;  
  1183.             String encodedString = "";  
  1184.             int currentLocation = 5;//rects.X + (rects.Width - getX()) / 2;  
  1185.             SolidBrush blackBrush = new SolidBrush(Color.Black);  
  1186.             SolidBrush witeBrush = new SolidBrush(Color.White);  
  1187.             int yTop = rects.Y;  
  1188.             for (int i = 0; i < strBarcode.Length; i++)  
  1189.             {  
  1190.                 encodedString = coded39Char[alphabet39.IndexOf(strBarcode[i])];  
  1191.   
  1192.                 for (int j = 0; j < 5; j++)  
  1193.                 {  
  1194.   
  1195.                     if (encodedString[j] == '0')  
  1196.                     {  
  1197.                         Rectangle re1 = new Rectangle(currentLocation, yTop, weight, BarcodeHeight);  
  1198.                         g.FillRectangle(blackBrush, re1);  
  1199.                         currentLocation += weight;  
  1200.                     }  
  1201.                     else  
  1202.                     {  
  1203.                         Rectangle re1 = new Rectangle(currentLocation, yTop, 3 * weight, BarcodeHeight);  
  1204.                         g.FillRectangle(blackBrush, re1);  
  1205.                         currentLocation += 3 * weight;  
  1206.                     }  
  1207.                     //画第6个     5   白条   
  1208.                     if ((j + 5) < 9)  
  1209.                     {  
  1210.                         if (encodedString[j + 5] == '0')  
  1211.                         {  
  1212.                             Rectangle re1 = new Rectangle(currentLocation, yTop, weight, BarcodeHeight);  
  1213.                             g.FillRectangle(witeBrush, re1);  
  1214.                             currentLocation += weight;  
  1215.                         }  
  1216.                         else  
  1217.                         {  
  1218.                             Rectangle re1 = new Rectangle(currentLocation, yTop, 3 * weight, BarcodeHeight);  
  1219.                             g.FillRectangle(witeBrush, re1);  
  1220.                             currentLocation += 3 * weight;  
  1221.                         }  
  1222.                     }  
  1223.                 }  
  1224.                 Rectangle re2 = new Rectangle(currentLocation, yTop, weight, BarcodeHeight);  
  1225.                 g.FillRectangle(witeBrush, re2);  
  1226.                 currentLocation += weight;  
  1227.             }  
  1228.   
  1229.   
  1230.         }  
  1231.   
  1232.         /// <summary>  
  1233.         /// 显示条形码和文字  
  1234.         /// </summary>  
  1235.         /// <param name="g"></param>  
  1236.         /// <param name="rects"></param>  
  1237.         public void DrawBarcode(Graphics g, Rectangle rects)  
  1238.         {  
  1239.             SizeF fsize = g.MeasureString(BarcodeText, this.footerFont);  
  1240.   
  1241.             Rectangle b = rects;  
  1242.             b.Y = rects.Y + rects.Height - (int)fsize.Height;  
  1243.             b.X = rects.X + (rects.Width - (int)fsize.Width) / 2;  
  1244.             b.Height = (int)fsize.Height;  
  1245.             DrawText(BarcodeText, g, b);  
  1246.             //BarCode  
  1247.             Rectangle m = new Rectangle();  
  1248.             m = rects;  
  1249.             m.Height = rects.Height - b.Height;  
  1250.             this.BarcodeHeight = m.Height;  
  1251.             DrawBitmap(g, m);  
  1252.         }  
  1253.   
  1254.         /// <summary>  
  1255.         /// 绘制条形码 无文字  
  1256.         /// </summary>  
  1257.         /// <param name="g"></param>  
  1258.         /// <param name="rects"></param>  
  1259.         public void DrawBarBit(Graphics g, Rectangle rects)  
  1260.         {  
  1261.             SizeF fsize = g.MeasureString(BarcodeText, this.footerFont);  
  1262.   
  1263.             Rectangle b = rects;  
  1264.             b.Y = rects.Y + rects.Height - (int)fsize.Height;  
  1265.             b.X = rects.X + (rects.Width - (int)fsize.Width) / 2;  
  1266.             b.Height = (int)fsize.Height;  
  1267.             //DrawText(BarcodeText, g, b);  
  1268.             //BarCode  
  1269.             Rectangle m = new Rectangle();  
  1270.             m = rects;  
  1271.             //m.Height = rects.Height - b.Height;  
  1272.             this.BarcodeHeight = m.Height;  
  1273.             DrawBitmap(g, m);  
  1274.         }  
  1275.         /// <summary>  
  1276.         /// 文本显示  
  1277.         /// </summary>  
  1278.         /// <param name="text"></param>  
  1279.         /// <param name="g"></param>  
  1280.         /// <param name="rects"></param>  
  1281.         protected void DrawText(string text, Graphics g, Rectangle rects)  
  1282.         {  
  1283.             g.DrawString(text, this.footerFont, Brushes.Black, rects);  
  1284.         }  
  1285.   
  1286.     }  
  1287.     #endregion  
  1288. }