资料来源:网络整理
时间:2023/2/14 0:05:03 共计:3622 浏览
这个要引用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
-
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
-
{
-
-
-
-
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;
-
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);
-
-
-
private TextAlign _drawfootertextalign = TextAlign.xlHAlignRight;
-
-
-
private List<System.Data.DataTable> _tables = new List<System.Data.DataTable>();
-
-
-
private Dictionary<string, float> _columnswidth = new Dictionary<string, float>();
-
-
-
private Dictionary<string, BorderWeightType> _columnsborder = new Dictionary<string, BorderWeightType>();
-
-
-
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;
-
-
-
private string _BarCodeText = String.Empty;
-
-
-
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
-
-
#region 属性
-
-
-
-
public virtual int RowIndex
-
{
-
get { return _rowindex; }
-
set
-
{
-
_rowindex = _saveindex = value;
-
}
-
}
-
-
-
-
-
public virtual string Title
-
{
-
get { return _title; }
-
set
-
{
-
_title = value;
-
IsDrawTitle = !string.IsNullOrEmpty(value);
-
}
-
}
-
-
-
-
-
public virtual string HeaderText
-
{
-
get { return _headerdtext; }
-
set
-
{
-
_headerdtext = value;
-
IsDrawHeader = !string.IsNullOrEmpty(value);
-
}
-
}
-
-
-
-
-
public virtual string FooterText
-
{
-
get { return _footertext; }
-
set
-
{
-
_footertext = value;
-
IsDrawFooter = !string.IsNullOrEmpty(value);
-
}
-
}
-
-
-
-
-
public virtual string SavePath
-
{
-
get { return _savepath; }
-
set
-
{
-
_savepath = value;
-
}
-
}
-
-
-
-
-
public virtual System.Drawing.Font TitleFont
-
{
-
get { return _titlefont; }
-
set { _titlefont = value; }
-
}
-
-
-
-
-
public virtual System.Drawing.Font HeaderFont
-
{
-
get { return _headerfont; }
-
set { _headerfont = value; }
-
}
-
-
-
-
-
public virtual System.Drawing.Font FooterFont
-
{
-
get { return _footerfont; }
-
set { _footerfont = value; }
-
}
-
-
-
-
-
public virtual System.Drawing.Font BodyHeaderFont
-
{
-
get { return _bodyheaderfont; }
-
set { _bodyheaderfont = value; }
-
}
-
-
-
-
-
public virtual System.Drawing.Font BodyFont
-
{
-
get { return _bodyfont; }
-
set { _bodyfont = value; }
-
}
-
-
-
-
-
public virtual List<System.Data.DataTable> Tables
-
{
-
get { return _tables; }
-
set { _tables = value; }
-
}
-
-
-
-
-
public virtual Dictionary<string, float> ColumnsWidth
-
{
-
get { return _columnswidth; }
-
set { _columnswidth = value; }
-
}
-
-
-
-
-
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; }
-
}
-
-
-
-
-
public virtual bool IsBodyListHeader
-
{
-
get { return _isbodylistheader; }
-
set { _isbodylistheader = value; }
-
}
-
-
-
-
-
public virtual bool IsDispalyBorderAll
-
{
-
get { return _isalldisplayborder; }
-
set { _isalldisplayborder = value; }
-
}
-
-
-
-
-
public virtual bool IsAutoConvertText
-
{
-
get { return _isautoconverttext; }
-
set
-
{
-
_isautoconverttext = value;
-
}
-
}
-
-
-
-
-
public virtual bool IsTitleAppendSheetName
-
{
-
get { return _isTitleAppendSheetName; }
-
set
-
{
-
_isTitleAppendSheetName = value;
-
}
-
}
-
-
-
-
-
public virtual BorderWeightType BorderWeight
-
{
-
get { return _borderweight; }
-
set { _borderweight = value; }
-
}
-
-
-
-
-
public virtual bool IsAutoFit { get; set; }
-
-
-
-
-
public virtual bool IsWrapText
-
{
-
get { return _iswraptext; }
-
set { _iswraptext = value; }
-
}
-
-
-
-
-
public string BarCodeText
-
{
-
get { return _BarCodeText; }
-
set
-
{
-
_BarCodeText = value;
-
if (!String.IsNullOrEmpty(value))
-
{
-
IsDrawTitle = true;
-
}
-
else
-
{
-
IsDrawTitle = !string.IsNullOrEmpty(_title);
-
}
-
}
-
}
-
-
-
-
-
public PaddingF PageMargin
-
{
-
get { return _pageMargin; }
-
set
-
{
-
_pageMargin = value;
-
}
-
}
-
-
-
-
-
public bool IsPrintFooter
-
{
-
get { return _isPrintFooter; }
-
set
-
{
-
_isPrintFooter = value;
-
}
-
}
-
-
-
-
-
-
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();
-
}
-
}
-
-
-
-
-
-
public void PrintExcel(bool isLandscape)
-
{
-
try
-
{
-
Microsoft.Office.Interop.Excel.Workbook wb = GetExcelWorkbook();
-
if (wb == null)
-
{
-
return;
-
}
-
-
foreach (var v in wb.Worksheets)
-
{
-
try
-
{
-
Microsoft.Office.Interop.Excel.Worksheet wsheet = ((Microsoft.Office.Interop.Excel.Worksheet)v);
-
-
if (SheetTable[wsheet.Name].Rows.Count > 0)
-
{
-
if (isLandscape)
-
{
-
-
wsheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
-
}
-
-
wsheet.PageSetup.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperA4;
-
-
wsheet.PageSetup.TopMargin = _pageMargin.Top;
-
wsheet.PageSetup.BottomMargin = _isPrintFooter ? 12.00 : _pageMargin.Bottom;
-
wsheet.PageSetup.LeftMargin = _pageMargin.Left;
-
wsheet.PageSetup.RightMargin = _pageMargin.Right;
-
-
-
-
wsheet.PageSetup.FooterMargin = 1;
-
if (_isPrintFooter)
-
{
-
-
wsheet.PageSetup.LeftFooter = wsheet.Name;
-
wsheet.PageSetup.CenterFooter = @"第 &P 页/共 &N 页";
-
wsheet.PageSetup.RightFooter = "打印时间:" + DateTime.Now.ToString();
-
}
-
-
wsheet.Columns.AutoFit();
-
if (!String.IsNullOrEmpty(_expandColumnName))
-
{
-
float swidth = 0;
-
float ywidth = 0;
-
foreach (var vk in _columnswidth)
-
{
-
object obj = ((Microsoft.Office.Interop.Excel.Range)wsheet.Columns[vk.Key, Type.Missing]).ColumnWidth;
-
swidth += float.Parse(obj.ToString());
-
ywidth += vk.Value;
-
}
-
-
-
Microsoft.Office.Interop.Excel.Range colrange = (Microsoft.Office.Interop.Excel.Range)wsheet.Columns[_expandColumnName, Type.Missing];
-
float cwidth = float.Parse(colrange.ColumnWidth.ToString());
-
if (swidth < ywidth)
-
{
-
colrange.ColumnWidth = (cwidth + (ywidth - swidth));
-
}
-
else
-
{
-
colrange.ColumnWidth = (cwidth - (swidth - ywidth));
-
}
-
}
-
-
wsheet.Rows.AutoFit();
-
-
wsheet.PrintOutEx();
-
}
-
}
-
catch
-
{ }
-
}
-
-
-
}
-
catch (Exception ex)
-
{
-
throw ex;
-
}
-
finally
-
{
-
Close();
-
}
-
}
-
-
-
-
-
public void PrintExcel()
-
{
-
PrintExcel(false);
-
}
-
-
-
-
-
-
-
public Microsoft.Office.Interop.Excel.Workbook GetExcelWorkbook()
-
{
-
if (_tables.Count == 0)
-
{
-
throw new Exception("Tables集合必须大于零!");
-
}
-
-
if (_rowindex < 0)
-
{
-
_rowindex = 0;
-
}
-
xlApp = new Microsoft.Office.Interop.Excel.Application();
-
xlApp.Visible = false;
-
xlApp.DisplayAlerts = false;
-
wbs = xlApp.Workbooks;
-
wb = wbs.Add(Missing.Value);
-
-
int tabcount = _tables.Count;
-
int sheets = wb.Worksheets.Count;
-
if (tabcount > sheets)
-
{
-
wb.Worksheets.Add(Missing.Value, Missing.Value, tabcount - sheets, Missing.Value);
-
}
-
-
-
-
WriteExcelSheet(wb);
-
-
-
-
return wb;
-
}
-
-
private void Close()
-
{
-
#region 关闭Excel进程
-
if (wb != null)
-
{
-
wb.Close(false, _savepath, Missing.Value);
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
-
wb = null;
-
}
-
if (wbs != null)
-
{
-
wbs.Close();
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(wbs);
-
wbs = null;
-
}
-
if (xlApp != null)
-
{
-
xlApp.Quit();
-
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
-
xlApp = null;
-
}
-
GC.Collect();
-
GC.WaitForPendingFinalizers();
-
#endregion
-
}
-
-
-
-
-
-
-
private void WriteExcelSheet(Microsoft.Office.Interop.Excel.Workbook wb)
-
{
-
List<string> SheetNames = GetSheetsName(wb);
-
for (int i = 0; i < _tables.Count; i++)
-
{
-
System.Data.DataTable table = _tables[i];
-
int rows = table.Rows.Count;
-
int cols = table.Columns.Count;
-
-
Microsoft.Office.Interop.Excel.Worksheet wsheet = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[i + 1];
-
-
wsheet.Select();
-
-
wsheet.Name = GetCorrectSheetName(SheetNames, table.TableName, (i + 1));
-
-
-
SheetTable.Add(wsheet.Name, table);
-
-
-
DrawTitle(wsheet, cols);
-
-
if (IsDrawHeader)
-
{
-
DrawText(wsheet, _headerdtext, _headerfont, TextAlign.xlHAlignLeft, cols);
-
}
-
-
-
if (_isbodylistheader)
-
{
-
_rowindex++;
-
Microsoft.Office.Interop.Excel.Range range = wsheet.Range[wsheet.Cells[_rowindex, 1], wsheet.Cells[_rowindex, cols]];
-
object[] header = new object[cols];
-
for (int j = 0; j < cols; j++)
-
{
-
header[j] = table.Columns[j].ToString();
-
}
-
range.Value2 = header;
-
SetFont(range, _bodyheaderfont);
-
if (_isalldisplayborder)
-
{
-
range.Borders.LineStyle = 1;
-
range.Borders.Weight = (int)_borderweight;
-
}
-
}
-
-
if (rows > 0)
-
{
-
_rowindex++;
-
Microsoft.Office.Interop.Excel.Range range = wsheet.get_Range("A" + _rowindex, Missing.Value);
-
object[,] objData = new Object[rows, cols];
-
for (int row = 0; row < rows; row++)
-
{
-
for (int col = 0; col < cols; col++)
-
{
-
string converttxt = String.Empty;
-
if (_isautoconverttext)
-
{
-
converttxt = "'";
-
}
-
objData[row, col] = converttxt + table.Rows[row][col].ToString();
-
}
-
}
-
range = range.get_Resize(rows, cols);
-
range.Value2 = objData;
-
SetFont(range, _bodyfont);
-
if (_isalldisplayborder)
-
{
-
range.Borders.LineStyle = 1;
-
range.Borders.Weight = (int)_borderweight;
-
}
-
else
-
{
-
-
-
-
foreach (var vk in _columnsborder)
-
{
-
Microsoft.Office.Interop.Excel.Borders borders = ((Microsoft.Office.Interop.Excel.Range)range.Columns[vk.Key, Type.Missing]).Borders;
-
borders.LineStyle = 1;
-
borders.Weight = (int)vk.Value;
-
}
-
}
-
if (IsAutoFit)
-
{
-
range.EntireColumn.AutoFit();
-
}
-
-
_rowindex += (rows - 1);
-
}
-
-
-
if (IsDrawFooter)
-
{
-
if (rows == 0)
-
{
-
_rowindex += 2;
-
}
-
DrawText(wsheet, _footertext, _footerfont,DrawFooterTextAlign, cols);
-
}
-
-
wsheet.Columns.WrapText = _iswraptext;
-
-
if (!IsAutoFit)
-
{
-
foreach (var vk in _columnswidth)
-
{
-
((Microsoft.Office.Interop.Excel.Range)wsheet.Columns[vk.Key, System.Type.Missing]).ColumnWidth = vk.Value;
-
}
-
}
-
-
SheetNames.Add(wsheet.Name);
-
-
_rowindex = _saveindex;
-
}
-
-
((Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1]).Select();
-
}
-
-
-
-
-
-
-
-
private void DrawTitle(Microsoft.Office.Interop.Excel.Worksheet wsheet, int colcount)
-
{
-
if (!IsDrawTitle)
-
{
-
return;
-
}
-
_rowindex++;
-
-
Microsoft.Office.Interop.Excel.Range cellrange = (Microsoft.Office.Interop.Excel.Range)wsheet.Cells[_rowindex, 1];
-
-
-
InsertImage(GetImagePath(), cellrange, wsheet);
-
-
-
string titletxt = _title;
-
if (_isTitleAppendSheetName)
-
{
-
titletxt += "(" + wsheet.Name + ")";
-
}
-
wsheet.Cells[_rowindex, 1] = titletxt;
-
-
Microsoft.Office.Interop.Excel.Range range = wsheet.Range[wsheet.Cells[_rowindex, 1], wsheet.Cells[_rowindex, colcount]];
-
-
SetFont(range, _titlefont);
-
-
-
Microsoft.Office.Interop.Excel.Border border = range.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom);
-
-
border.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium;
-
-
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenterAcrossSelection;
-
-
range.Merge(range.MergeCells);
-
range.WrapText = true;
-
}
-
-
-
-
-
-
-
-
private void DrawText(Microsoft.Office.Interop.Excel.Worksheet wsheet, string txt,System.Drawing.Font font,TextAlign align, int colcount)
-
{
-
_rowindex++;
-
-
wsheet.Cells[_rowindex, 1] = txt;
-
-
Microsoft.Office.Interop.Excel.Range range = wsheet.Range[wsheet.Cells[_rowindex, 1], wsheet.Cells[_rowindex, colcount]];
-
SetFont(range, font);
-
-
range.HorizontalAlignment = (int)align;
-
-
range.Merge(range.MergeCells);
-
}
-
-
-
-
-
-
-
-
private void InsertImage(string imgpath, Microsoft.Office.Interop.Excel.Range range, Microsoft.Office.Interop.Excel.Worksheet wsheet)
-
{
-
if (System.String.IsNullOrEmpty(imgpath))
-
{
-
return;
-
}
-
try
-
{
-
-
range.Select();
-
float PicLeft, PicTop;
-
PicLeft = Convert.ToSingle(range.Left);
-
PicTop = Convert.ToSingle(range.Top) + 1.5F;
-
wsheet.Shapes.AddPicture(imgpath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue, PicLeft, PicTop, -1, -1);
-
}
-
catch { }
-
}
-
-
-
-
-
-
-
private void SetFont(Microsoft.Office.Interop.Excel.Range range, System.Drawing.Font font)
-
{
-
range.Font.Bold = font.Bold;
-
range.Font.Italic = font.Italic;
-
range.Font.Name = font.Name;
-
range.Font.Size = font.Size;
-
range.Font.Underline = font.Underline;
-
}
-
-
-
-
-
-
-
private List<string> GetSheetsName(Microsoft.Office.Interop.Excel.Workbook wb)
-
{
-
List<string> names = new List<string>();
-
foreach (var sheet in wb.Worksheets)
-
{
-
string name = ((Microsoft.Office.Interop.Excel.Worksheet)sheet).Name;
-
if (!String.IsNullOrEmpty(name))
-
{
-
names.Add(name);
-
}
-
}
-
return names;
-
}
-
-
-
-
-
-
private string GetCorrectSheetName(List<string> existnames, string tabname, int tabindex)
-
{
-
string name = String.Empty;
-
-
-
if (!VerifySheetName(tabname))
-
{
-
tabname = "Table" + tabindex;
-
}
-
-
bool isexist = existnames.Exists(n => n.Equals(tabname));
-
if (isexist)
-
{
-
tabindex++;
-
name = GetCorrectSheetName(existnames, "Table" + tabindex, tabindex);
-
}
-
else
-
{
-
name = tabname;
-
}
-
return name;
-
}
-
-
-
-
-
private bool VerifySheetName(string sheetname)
-
{
-
if (string.IsNullOrEmpty(sheetname) || sheetname.Length > 31)
-
{
-
return false;
-
}
-
-
if (sheetname.Contains("\\") || sheetname.Contains("/"))
-
{
-
return false;
-
}
-
-
if (sheetname.Contains(":") || sheetname.Contains(":"))
-
{
-
return false;
-
}
-
-
if (sheetname.Contains("?") || sheetname.Contains("?"))
-
{
-
return false;
-
}
-
-
if (sheetname.Contains("[") || sheetname.Contains("]"))
-
{
-
return false;
-
}
-
return true;
-
}
-
-
-
-
-
-
public string GetImagePath()
-
{
-
if (String.IsNullOrEmpty(BarCodeText))
-
{
-
return String.Empty;
-
}
-
string imgpath = String.Empty;
-
Bitmap bitmap = null;
-
Graphics gs = null;
-
try
-
{
-
bitmap = WMS.UI.Properties.Resources.BarCode_Template;
-
gs = Graphics.FromImage(bitmap);
-
BarCode39 barcode = new BarCode39();
-
barcode.BarcodeText = BarCodeText;
-
barcode.DrawBarBit(gs, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
-
imgpath = Application.StartupPath + "\\BarCode.jpg";
-
bitmap.Save(imgpath);
-
}
-
catch
-
{
-
imgpath = "";
-
}
-
finally
-
{
-
if (gs != null)
-
{
-
gs.Dispose();
-
gs = null;
-
}
-
if (bitmap != null)
-
{
-
bitmap.Dispose();
-
bitmap = null;
-
}
-
}
-
return imgpath;
-
}
-
}
-
-
public enum BorderWeightType
-
{
-
-
-
-
xlMedium = -4138,
-
-
-
-
xlHairline = 1,
-
-
-
-
xlThin = 2,
-
-
-
-
xlThick = 4,
-
}
-
-
public enum BorderLineStyle
-
{
-
-
-
-
xlLineStyleNone = -4142,
-
-
-
-
xlDouble = -4119,
-
-
-
-
xlDot = -4118,
-
-
-
-
xlDash = -4115,
-
-
-
-
xlContinuous = 1,
-
-
-
-
xlDashDot = 4,
-
-
-
-
xlDashDotDot = 5,
-
-
-
-
xlSlantDashDot = 13,
-
}
-
-
public enum TextAlign
-
{
-
xlHAlignRight = -4152,
-
xlHAlignLeft = -4131,
-
xlHAlignJustify = -4130,
-
xlHAlignDistributed = -4117,
-
xlHAlignCenter = -4108,
-
xlHAlignGeneral = 1,
-
xlHAlignFill = 5,
-
xlHAlignCenterAcrossSelection = 7,
-
}
-
-
public struct PaddingF
-
{
-
double _Left;
-
double _Right;
-
double _Top;
-
double _Bottom;
-
-
public PaddingF(double all)
-
{
-
this._Left = all;
-
this._Right = all;
-
this._Top = all;
-
this._Bottom = all;
-
}
-
-
public PaddingF(double left, double top, double right, double bottom)
-
{
-
this._Left = left;
-
this._Right = right;
-
this._Top = top;
-
this._Bottom = bottom;
-
}
-
-
public void SetValue(double all)
-
{
-
SetValue(all, all, all, all);
-
}
-
-
public void SetValue(double top, double left, double right, double bottom)
-
{
-
this._Left = left;
-
this._Right = right;
-
this._Top = top;
-
this._Bottom = bottom;
-
}
-
-
public double Left
-
{
-
get { return _Left; }
-
set
-
{
-
_Left = value;
-
}
-
}
-
-
public double Right
-
{
-
get { return _Right; }
-
set
-
{
-
_Right = value;
-
}
-
}
-
-
public double Top
-
{
-
get { return _Top; }
-
set
-
{
-
_Top = value;
-
}
-
}
-
-
public double Bottom
-
{
-
get { return _Bottom; }
-
set
-
{
-
_Bottom = value;
-
}
-
}
-
}
-
-
#region BarCode 条形码
-
public class BarCode39
-
{
-
public string BarcodeText = string.Empty;
-
-
public int BarcodeHeight = 0;
-
public int BarcodeWidth = 0;
-
-
public Font footerFont = new Font("微软雅黑", 13.0f);
-
-
private double wideToNarrowRatio = 3.0;
-
-
public double WideToNarrowRatio
-
{
-
get { return wideToNarrowRatio; }
-
set { wideToNarrowRatio = value; }
-
}
-
private int weight = 1;
-
-
public int Weight
-
{
-
get { return weight; }
-
set { weight = value; }
-
}
-
-
-
-
private String alphabet39 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*";
-
-
#region
-
private String[] coded39Char =
-
{
-
"001100100",
-
"100010100",
-
"010010100",
-
"110000100",
-
"001010100",
-
"101000100",
-
"011000100",
-
"000110100",
-
"100100100",
-
"010100100",
-
"100010010",
-
"010010010",
-
"110000010",
-
"001010010",
-
"101000010",
-
"011000010",
-
"000110010",
-
"100100010",
-
"010100010",
-
"001100010",
-
"100010001",
-
"010010001",
-
"110000001",
-
"001010001",
-
"101000001",
-
"011000001",
-
"000110001",
-
"100100001",
-
"010100001",
-
"001100001",
-
"100011000",
-
"010011000",
-
"110001000",
-
"001011000",
-
"101001000",
-
"011001000",
-
"000111000",
-
"110000100",
-
"011000100",
-
"010101000",
-
"010100010",
-
"010001010",
-
"100101000",
-
"001101000"
-
};
-
#endregion
-
-
public BarCode39()
-
{
-
BarcodeText = "1234";
-
}
-
-
-
-
-
private int getX()
-
{
-
int currentLocation = 0;
-
string strBarcode = "*" + BarcodeText.ToUpper() + "*";
-
for (int i = 0; i < strBarcode.Length; i++)
-
{
-
string encodedString = coded39Char[alphabet39.IndexOf(strBarcode[i])];
-
-
for (int j = 0; j < 5; j++)
-
{
-
-
if (encodedString[j] == '0')
-
{
-
currentLocation += weight;
-
}
-
else
-
{
-
currentLocation += 3 * weight;
-
}
-
-
if ((j + 5) < 9)
-
{
-
if (encodedString[j + 5] == '0')
-
{
-
currentLocation += weight;
-
}
-
else
-
{
-
currentLocation += 3 * weight;
-
}
-
}
-
}
-
currentLocation += weight;
-
}
-
return currentLocation;
-
}
-
-
-
-
-
-
protected void DrawBitmap(Graphics g, Rectangle rects)
-
{
-
if (BarcodeText == "") return;
-
string strBarcode = "*" + BarcodeText.ToUpper() + "*";
-
-
String encodedString = "";
-
int currentLocation = 5;
-
SolidBrush blackBrush = new SolidBrush(Color.Black);
-
SolidBrush witeBrush = new SolidBrush(Color.White);
-
int yTop = rects.Y;
-
for (int i = 0; i < strBarcode.Length; i++)
-
{
-
encodedString = coded39Char[alphabet39.IndexOf(strBarcode[i])];
-
-
for (int j = 0; j < 5; j++)
-
{
-
-
if (encodedString[j] == '0')
-
{
-
Rectangle re1 = new Rectangle(currentLocation, yTop, weight, BarcodeHeight);
-
g.FillRectangle(blackBrush, re1);
-
currentLocation += weight;
-
}
-
else
-
{
-
Rectangle re1 = new Rectangle(currentLocation, yTop, 3 * weight, BarcodeHeight);
-
g.FillRectangle(blackBrush, re1);
-
currentLocation += 3 * weight;
-
}
-
-
if ((j + 5) < 9)
-
{
-
if (encodedString[j + 5] == '0')
-
{
-
Rectangle re1 = new Rectangle(currentLocation, yTop, weight, BarcodeHeight);
-
g.FillRectangle(witeBrush, re1);
-
currentLocation += weight;
-
}
-
else
-
{
-
Rectangle re1 = new Rectangle(currentLocation, yTop, 3 * weight, BarcodeHeight);
-
g.FillRectangle(witeBrush, re1);
-
currentLocation += 3 * weight;
-
}
-
}
-
}
-
Rectangle re2 = new Rectangle(currentLocation, yTop, weight, BarcodeHeight);
-
g.FillRectangle(witeBrush, re2);
-
currentLocation += weight;
-
}
-
-
-
}
-
-
-
-
-
-
-
public void DrawBarcode(Graphics g, Rectangle rects)
-
{
-
SizeF fsize = g.MeasureString(BarcodeText, this.footerFont);
-
-
Rectangle b = rects;
-
b.Y = rects.Y + rects.Height - (int)fsize.Height;
-
b.X = rects.X + (rects.Width - (int)fsize.Width) / 2;
-
b.Height = (int)fsize.Height;
-
DrawText(BarcodeText, g, b);
-
-
Rectangle m = new Rectangle();
-
m = rects;
-
m.Height = rects.Height - b.Height;
-
this.BarcodeHeight = m.Height;
-
DrawBitmap(g, m);
-
}
-
-
-
-
-
-
-
public void DrawBarBit(Graphics g, Rectangle rects)
-
{
-
SizeF fsize = g.MeasureString(BarcodeText, this.footerFont);
-
-
Rectangle b = rects;
-
b.Y = rects.Y + rects.Height - (int)fsize.Height;
-
b.X = rects.X + (rects.Width - (int)fsize.Width) / 2;
-
b.Height = (int)fsize.Height;
-
-
-
Rectangle m = new Rectangle();
-
m = rects;
-
-
this.BarcodeHeight = m.Height;
-
DrawBitmap(g, m);
-
}
-
-
-
-
-
-
-
protected void DrawText(string text, Graphics g, Rectangle rects)
-
{
-
g.DrawString(text, this.footerFont, Brushes.Black, rects);
-
}
-
-
}
-
#endregion
-
}
-
版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。