使用C# 編輯Word文檔 繪製標籤
環境配置
Visual studio 2013
Office Word 2013
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;
Microsoft.Office.Interop.Word._Application app =
new Microsoft.Office.Interop.Word.Application();
app.Visible = true;
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;
Microsoft.Office.Interop.Word.Table format = app.Selection.Tables.Add(range, 5, 2, ref missValue, ref missValue);
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;
Microsoft.Office.Interop.Word.Table label = app.Selection.Tables.Add(cell, 1, 3, ref missValue, ref missValue);
label.Rows.Add(ref missValue);
label.Rows.Add(ref missValue);
label.Rows.Add(ref missValue);
label.Rows.Add(ref missValue);
label.Rows.Add(ref missValue);
label.Rows.Add(ref missValue);
label.Rows.Add(ref missValue);
label.Rows.Add(ref missValue);
label.Rows.Add(ref missValue);
{
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;
label_items.Height = 13F;
label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;
}
{
label.Cell(1, 2).Merge(label.Cell(1, 3));
label.Cell(2, 2).Merge(label.Cell(2, 3));
}
{
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;
label_items.Height = CONST_TITLE_FONT_CELL;
label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;
}
{
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;
}
{
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;
label_items.Height = CONST_TITLE_FONT_CELL;
label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;
}
{
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;
label_items.Height = CONST_TITLE_FONT_CELL;
label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;
}
{
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;
}
{
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;
}
{
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;
label_items.Height = CONST_TITLE_FONT_CELL;
label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;
}
{
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;
label_items.Height = CONST_TITLE_FONT_CELL;
label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;
}
{
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;
}
{
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;
}
{
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;
label_items.Height = CONST_TITLE_FONT_CELL;
label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;
}
{
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;
label_items.Height = CONST_TITLE_FONT_CELL;
label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;
}
{
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;
}
{
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;
}
{
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;
label_items.Height = CONST_TITLE_FONT_CELL;
label_items.HeightRule = WdRowHeightRule.wdRowHeightExactly;
}
{
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;
app.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);
}
}
}