1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 使用C# 编辑Word文档 绘制标签

使用C# 编辑Word文档 绘制标签

时间:2024-04-17 23:16:41

相关推荐

使用C# 编辑Word文档 绘制标签

使用C# 编辑Word文档 绘制标签

环境配置

Visual studio Office Word C#

标签携带信息及宏定义

class LabelItem{public string label_top {get; set; }public string label_left {get; set; }public string label_right {get; set; }public string material_name {get; set; }public string weight {get; set; }public string item_code {get; set; }public string inspected_date {get; set; }public string expration_date {get; set; }public string inspector {get; set; }public string storage_status {get; set; }public string qrcode_value {get; set; }public string bar39_value {get; set; }}

private const string CONST_MATERIAL_NAME = "MATERIAL NAME";private const string CONST_WEIGHT = "WEIGHT";private const string CONST_ITEM_CODE = "ITEM CODE";private const string CONST_INSPECTED_DATE = "INSPECTED DATE";private const string CONST_EXPRATION_DATE = "EXPRATION DATE";private const string CONST_INSPECTOR = "INSPECTOR";private const string CONST_STORAGE_STATUS = "STORAGE STATUS";private const string CONST_EN_FONT_NAME = "Calibri";private const int CONST_TITLE_FONT_SIZE = 8;private const float CONST_TITLE_FONT_CELL = 10L;private const int CONST_VALUE_FONT_SIZE = 11;private const float CONST_VALUE_FONT_CELL = 15L;

标签绘制

private static void LabelWord(object obj){object missValue = System.Reflection.Missing.Value;// 创建Word应用程序Microsoft.Office.Interop.Word._Application app =new Microsoft.Office.Interop.Word.Application();app.Visible = true;// 添加Word文档Microsoft.Office.Interop.Word._Document doc =app.Documents.Add();// 设置页边距doc.PageSetup.LeftMargin = 10F;doc.PageSetup.RightMargin = 10F;doc.PageSetup.TopMargin = 0;doc.PageSetup.BottomMargin = 0;// 换行app.Selection.TypeParagraph();// 单元格对象Microsoft.Office.Interop.Word.Range range = app.Selection.Range;// 表格对象 5行2列Microsoft.Office.Interop.Word.Table format = app.Selection.Tables.Add(range, 5, 2, ref missValue, ref missValue);//1-10 标签format.Cell(1, 1).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDashDot;format.Cell(2, 1).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDashDot;format.Cell(3, 1).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDashDot;format.Cell(4, 1).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDashDot;format.Cell(5, 1).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDashDot;format.Cell(1, 2).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDashDot;format.Cell(2, 2).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDashDot;format.Cell(3, 2).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDashDot;format.Cell(4, 2).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDashDot;format.Cell(5, 2).Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleDashDot;LabelItem item = new LabelItem();item.label_top = "-------";item.material_name = "-------";item.weight = "-------";item.item_code = "-------";item.inspected_date = "-------";item.expration_date = "-------";item.inspector = "-------";item.storage_status = "-------";item.label_left = "-------";item.label_right = "-------";item.qrcode_value = "-------";item.bar39_value = "-------";var cell = format.Cell(1, 1).Range;//Row 1Microsoft.Office.Interop.Word.Table label = app.Selection.Tables.Add(cell, 1, 3, ref missValue, ref missValue);//Row 2label.Rows.Add(ref missValue);//Row 3label.Rows.Add(ref missValue);//Row 4label.Rows.Add(ref missValue);//Row 5label.Rows.Add(ref missValue);//Row 6label.Rows.Add(ref missValue);//Row 7label.Rows.Add(ref missValue);//Row 8label.Rows.Add(ref missValue);//Row 9label.Rows.Add(ref missValue);//Row 10label.Rows.Add(ref missValue);// for label_top{var label_items = label.Cell(1, 1);label_items.Range.Text = item.label_top;label_items.Range.Font.Size = 10;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Range.Bold = 1; //TRUElabel_items.Height = 13F;//固定值label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}// for bar39 code;{label.Cell(1, 2).Merge(label.Cell(1, 3));label.Cell(2, 2).Merge(label.Cell(2, 3));}// for material name{var label_items = label.Cell(2, 1);label_items.Range.Text = CONST_MATERIAL_NAME;label_items.Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone;label_items.Range.Font.Size = CONST_TITLE_FONT_SIZE;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Range.Font.Bold = 1; //TRUElabel_items.Height = CONST_TITLE_FONT_CELL;label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}// for material name value{var label_items = label.Cell(3, 1);label_items.Range.Text = item.material_name;label_items.Range.Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Font.Size = CONST_VALUE_FONT_SIZE;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Height = CONST_VALUE_FONT_CELL;label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}{var label_items = label.Cell(3, 2);label_items.Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleNone;}// for weight{var label_items = label.Cell(4, 1);label_items.Range.Text = CONST_WEIGHT;label_items.Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone;label_items.Range.Font.Size = CONST_TITLE_FONT_SIZE;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Range.Font.Bold = 1; //TRUElabel_items.Height = CONST_TITLE_FONT_CELL;label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}// for item code{var label_items = label.Cell(4, 2);label_items.Range.Text = CONST_ITEM_CODE;label_items.Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone;label_items.Range.Font.Size = CONST_TITLE_FONT_SIZE;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Range.Font.Bold = 1; //TRUElabel_items.Height = CONST_TITLE_FONT_CELL;label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}// for weight value{var label_items = label.Cell(5, 1);label_items.Range.Text = item.weight;label_items.Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleNone;label_items.Range.Font.Size = CONST_VALUE_FONT_SIZE;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Height = CONST_VALUE_FONT_CELL;label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}// for item code value{var label_items = label.Cell(5, 2);label_items.Range.Text = item.item_code;label_items.Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleNone;label_items.Range.Font.Size = CONST_VALUE_FONT_SIZE;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Height = CONST_VALUE_FONT_CELL;label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}// for inspected date{var label_items = label.Cell(6, 1);label_items.Range.Text = CONST_INSPECTED_DATE;label_items.Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone;label_items.Range.Font.Size = CONST_TITLE_FONT_SIZE;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Range.Font.Bold = 1; //TRUElabel_items.Height = CONST_TITLE_FONT_CELL;label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}// for item code{var label_items = label.Cell(6, 2);label_items.Range.Text = CONST_EXPRATION_DATE;label_items.Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone;label_items.Range.Font.Size = CONST_TITLE_FONT_SIZE;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Range.Font.Bold = 1; //TRUElabel_items.Height = CONST_TITLE_FONT_CELL;label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}// for inspected date value{var label_items = label.Cell(7, 1);label_items.Range.Text = item.inspected_date;label_items.Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleNone;label_items.Range.Font.Size = CONST_VALUE_FONT_SIZE;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Height = CONST_VALUE_FONT_CELL;label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}// for expration date value{var label_items = label.Cell(7, 2);label_items.Range.Text = item.expration_date;label_items.Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleNone;label_items.Range.Font.Size = CONST_VALUE_FONT_SIZE;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Height = CONST_VALUE_FONT_CELL;label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}// for inspector{var label_items = label.Cell(8, 1);label_items.Range.Text = CONST_INSPECTOR;label_items.Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone;label_items.Range.Font.Size = CONST_TITLE_FONT_SIZE;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Range.Font.Bold = 1; //TRUElabel_items.Height = CONST_TITLE_FONT_CELL;label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}// for storage status{var label_items = label.Cell(8, 2);label_items.Range.Text = CONST_STORAGE_STATUS;label_items.Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone;label_items.Range.Font.Size = CONST_TITLE_FONT_SIZE;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Range.Font.Bold = 1; //TRUElabel_items.Height = CONST_TITLE_FONT_CELL;label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}// for inspector value{var label_items = label.Cell(9, 1);label_items.Range.Text = item.inspector;label_items.Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleNone;label_items.Range.Font.Size = CONST_VALUE_FONT_SIZE;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Height = CONST_VALUE_FONT_CELL;label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}// for storage status value{var label_items = label.Cell(9, 2);label_items.Range.Text = item.storage_status;label_items.Range.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;label_items.Range.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleNone;label_items.Range.Font.Size = CONST_VALUE_FONT_SIZE;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Height = CONST_VALUE_FONT_CELL;label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}// for label_left{var label_items = label.Cell(10, 1);label_items.Range.Text = item.label_left;label_items.Range.Font.Size = CONST_TITLE_FONT_SIZE;label_items.Range.Font.Name = CONST_EN_FONT_NAME;label_items.Range.Font.Bold = 1; //TRUElabel_items.Height = CONST_TITLE_FONT_CELL;label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;}// for OK{label.Cell(3, 3).Select();app.Selection.MoveDown(WdUnits.wdLine, 2, WdMovementType.wdExtend);app.Selection.Cells.Merge();app.Selection.Range.Font.Size = 36;app.Selection.Range.Font.Name = CONST_EN_FONT_NAME;app.Selection.Range.Font.Bold = 1; //TRUEapp.Selection.Text = "OK";app.Selection.Range.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleSingle;app.Selection.Range.Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleSingle;app.Selection.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;}{label.Cell(6, 3).Select();app.Selection.MoveDown(WdUnits.wdLine, 3, WdMovementType.wdExtend);app.Selection.Cells.Merge();app.Selection.Range.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleSingle;app.Selection.Range.Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleSingle;}{var bar39= doc.Shapes.AddOLEControl("BARCODE.BarCodeCtrl", 110, 8).OLEFormat.Object;bar39.Style = 6;bar39.Validation = 1;bar39.Value = item.bar39_value;bar39.LineWeight = 2;doc.ToggleFormsDesign();doc.Shapes["Control 2"].Select();app.Selection.ShapeRange.ScaleHeight(0.45F, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoScaleFrom.msoScaleFromBottomRight);app.Selection.ShapeRange.IncrementLeft(15F);app.Selection.ShapeRange.IncrementTop(-9.75F);app.Selection.ShapeRange.ScaleWidth(1.23F, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoScaleFrom.msoScaleFromTopLeft);app.Selection.ShapeRange.ScaleHeight(1.11F, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoScaleFrom.msoScaleFromTopLeft);}{var bar39 = doc.Shapes.AddOLEControl("BARCODE.BarCodeCtrl", 200, 50).OLEFormat.Object;bar39.Style = 11;bar39.Value = item.qrcode_value;bar39.LineWeight = 2;doc.ToggleFormsDesign();doc.Shapes["Control 3"].Select();app.Selection.ShapeRange.ScaleWidth(0.55F, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoScaleFrom.msoScaleFromBottomRight);app.Selection.ShapeRange.ScaleHeight(0.55F, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoScaleFrom.msoScaleFromBottomRight);app.Selection.ShapeRange.IncrementLeft(-13.5F);app.Selection.ShapeRange.IncrementTop(-10.5F);}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。