XLSX: Программное редактирование существующего XLSX документа с помощью низко-уровневого редактора.
В этом разделе
Класс
XlsxDocumentEditor является низко-уровневым редактором для существующего XLSX документа, т.е. класс позволяет редактировать XLSX документ с точки зрения документа Open XML.
Класс
XlsxDocumentEditor предназначен для быстрой и простой разработки генератора отчетов/счетов на основе шаблона XLSX документа.
Если вы хотите создать отчет/счет в формате XLSX, вам необходимо выполнить следующие шаги:
-
Открыть Microsoft Excel:
- Создать новый XLSX документ или открыть существующий XLSX документ. Документ XLSX будет использоваться в качестве шаблона для отчета/счета.
- Изменить содержимое XLSX документа. Документ должен содержать всю необходимую разметку, кроме динамических данных. Места для динамических данных должны быть отмечены уникальными текстовыми константами.
-
В коде:
- Открыть документ-шаблон с помощью класса XlsxDocumentEditor и добавить в документ динамические данные (замените текст, вставьте строки таблицы, замените изображения и т. д.).
- Сохранить измененный документ в файл XLSX с помощью метода OpenXmlDocumentEditor.Save или экспортировать измененный документ с помощью метода OpenXmlDocumentEditor.Export в любой формат, поддерживаемый VintaSoft Imaging SDK (PDF, PDF/A, TIFF, ...).
Проект "Report Generator Demo"
содержит несколько примеров, демонстрирующих, как создавать отчеты на основе XLSX шаблона.
Класс XlsxDocumentEditor
Класс
XlsxDocumentEditor позволяет:
Класс OpenXmlDocumentElement
OpenXmlDocumentElement представляет базовый класс для всех элементов в DOCX или XLSX документе и позволяет:
Вот C#/VB.NET код, который демонстрирует, как найти и заменить текст в XLSX документе (пример использует документ-шаблон
FindAndReplaceText_template.xlsx
):
public static void XlsxFindAndReplaceTextExample()
{
string templateFilename = "FindAndReplaceText_template.xlsx";
string outFilename = "FindAndReplaceText.xlsx";
string outPdfFilename = "FindAndReplaceText.pdf";
// create XlsxDocumentEditor that allows to edit file "FindAndReplaceText_template.xlsx"
using (Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor editor =
new Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor(templateFilename))
{
// get first sheet in XLSX document
Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheet sheet = editor.Sheets[0];
// replace first occurrence of text "[field1]" by text "value1"
sheet["[field1]"] = "value1";
// replace all occurrences of text "[field2]" by text "value2"
sheet.ReplaceText("[field2]", "value2");
// find text content that corresponds to the text "[field3]"
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextContent field3Content = sheet.FindText("[field3]");
// change text in found text content
field3Content.Text = "value3";
// replace text "[multiline_field]" by multiline text
sheet["[multiline_field]"] = "\nline1\nline2\nline3";
// save changed document to a XLSX file
editor.Save(outFilename);
// export changed document to a PDF document
editor.Export(outPdfFilename);
}
}
Public Shared Sub XlsxFindAndReplaceTextExample()
Dim templateFilename As String = "FindAndReplaceText_template.xlsx"
Dim outFilename As String = "FindAndReplaceText.xlsx"
Dim outPdfFilename As String = "FindAndReplaceText.pdf"
' create XlsxDocumentEditor that allows to edit file "FindAndReplaceText_template.xlsx"
Using editor As New Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor(templateFilename)
' get first sheet in XLSX document
Dim sheet As Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheet = editor.Sheets(0)
' replace first occurrence of text "[field1]" by text "value1"
sheet("[field1]") = "value1"
' replace all occurrences of text "[field2]" by text "value2"
sheet.ReplaceText("[field2]", "value2")
' find text content that corresponds to the text "[field3]"
Dim field3Content As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextContent = sheet.FindText("[field3]")
' change text in found text content
field3Content.Text = "value3"
' replace text "[multiline_field]" by multiline text
sheet("[multiline_field]") = vbLf & "line1" & vbLf & "line2" & vbLf & "line3"
' save changed document to a XLSX file
editor.Save(outFilename)
' export changed document to a PDF document
editor.Export(outPdfFilename)
End Using
End Sub
Класс OpenXmlTextContent
OpenXmlTextContent представляет текстовое содержимое DOCX или XLSX документа и позволяет:
Вот C#/VB.NET код, который демонстрирует, как изменять свойства текста текстового содержимого (в примере используется шаблон документа
ChangeTextProperties_template.xlsx
):
public static void XlsxChangeTextPropertiesExample()
{
string templateFilename = "ChangeTextProperties_template.xlsx";
string outFilename = "ChangeTextProperties.xlsx";
string outPdfFilename = "ChangeTextProperties.pdf";
// create XlsxDocumentEditor that allows to edit file "ChangeTextProperties_template.xlsx"
using (Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor editor =
new Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor(templateFilename))
{
// get first sheet in XLSX document
Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheet sheet = editor.Sheets[0];
// set text color
sheet.FindText("COLOR").Substring(0, 2).SetTextProperties(CreateColorTextProperties(System.Drawing.Color.Red));
sheet.FindText("COLOR").Substring(2, 1).SetTextProperties(CreateColorTextProperties(System.Drawing.Color.Green));
sheet.FindText("COLOR").Substring(3, 2).SetTextProperties(CreateColorTextProperties(System.Drawing.Color.Blue));
// set text "bold text" as bold text
sheet.FindText("bold text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.BoldText);
// set text "italic text" as italic text
sheet.FindText("italic text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.ItalicText);
// set text "underline text" as underline text
sheet.FindText("underline text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.UnderlineText);
// set text "strike text" as striked out text
sheet.FindText("strike text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.StrikeText);
// change font size
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties setTextSize =
new Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties();
setTextSize.FontSize = 16;
sheet.FindText("text with size 16pt").Substring(0, 4).SetTextProperties(setTextSize);
// save changed document to a XLSX file
editor.Save(outFilename);
// export changed document to a PDF document
editor.Export(outPdfFilename);
}
}
/// <summary>
/// Creates the color text properties.
/// </summary>
/// <param name="color">The color.</param>
private static Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties CreateColorTextProperties(System.Drawing.Color color)
{
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties result =
new Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties();
result.Color = color;
return result;
}
Public Shared Sub XlsxChangeTextPropertiesExample()
Dim templateFilename As String = "ChangeTextProperties_template.xlsx"
Dim outFilename As String = "ChangeTextProperties.xlsx"
Dim outPdfFilename As String = "ChangeTextProperties.pdf"
' create XlsxDocumentEditor that allows to edit file "ChangeTextProperties_template.xlsx"
Using editor As New Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor(templateFilename)
' get first sheet in XLSX document
Dim sheet As Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheet = editor.Sheets(0)
' set text color
sheet.FindText("COLOR").Substring(0, 2).SetTextProperties(CreateColorTextProperties(System.Drawing.Color.Red))
sheet.FindText("COLOR").Substring(2, 1).SetTextProperties(CreateColorTextProperties(System.Drawing.Color.Green))
sheet.FindText("COLOR").Substring(3, 2).SetTextProperties(CreateColorTextProperties(System.Drawing.Color.Blue))
' set text "bold text" as bold text
sheet.FindText("bold text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.BoldText)
' set text "italic text" as italic text
sheet.FindText("italic text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.ItalicText)
' set text "underline text" as underline text
sheet.FindText("underline text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.UnderlineText)
' set text "strike text" as striked out text
sheet.FindText("strike text").SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.StrikeText)
' change font size
Dim setTextSize As New Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties()
setTextSize.FontSize = 16
sheet.FindText("text with size 16pt").Substring(0, 4).SetTextProperties(setTextSize)
' save changed document to a XLSX file
editor.Save(outFilename)
' export changed document to a PDF document
editor.Export(outPdfFilename)
End Using
End Sub
''' <summary>
''' Creates the color text properties.
''' </summary>
''' <param name="color">The color.</param>
Private Shared Function CreateColorTextProperties(color As System.Drawing.Color) As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties
Dim result As New Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties()
result.Color = color
Return result
End Function
Класс XlsxDocumentSheet
XlsxDocumentSheet представляет лист XLSX документа и позволяет:
Класс XlsxDocumentSheetRow
XlsxDocumentSheetRow представляет строку листа XLSX и позволяет:
Вот C#/VB.NET код, который демонстрирует, как заполнить таблицу данных (в примере используется документ-шаблон
CopyTableRow_template.xlsx
):
public static void XlsxCopyTableRowExample()
{
string templateFilename = "CopyTableRow_template.xlsx";
string outFilename = "CopyTableRow.xlsx";
string outPdfFilename = "CopyTableRow.pdf";
// create XlsxDocumentEditor that allows to edit file "CopyTableRow_template.xlsx"
using (Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor editor =
new Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor(templateFilename))
{
// get first sheet in XLSX document
Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheet sheet = editor.Sheets[0];
// find row that contains text "[cell1]"
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableRow templateRow = sheet.FindRow("[cell1]");
// create an array that contains colors
System.Drawing.Color[] colors = new System.Drawing.Color[] {
System.Drawing.Color.Red,
System.Drawing.Color.Green,
System.Drawing.Color.Blue,
System.Drawing.Color.Orange,
System.Drawing.Color.Yellow
};
// for each color
for (int i = 0; i < colors.Length; i++)
{
// insert copy of template row before template row
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableRow rowCopy =
(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableRow)templateRow.InsertCopyBeforeSelf();
// set row data
rowCopy[0].Text = string.Format("Copy {0} ({1})", i, colors[i]);
rowCopy["[cell1]"] = string.Format("cell data {0}", i);
// set cell colors
rowCopy[1].SetFillColor(colors[i]);
rowCopy[2].SetFillColor(colors[i]);
// if color has odd index in colors array
if (i % 2 == 1)
{
// set row height to 10mm
rowCopy.Height = Vintasoft.Imaging.Utils.UnitOfMeasureConverter.ConvertToPoints(10, Vintasoft.Imaging.UnitOfMeasure.Millimeters);
}
}
// remove template row
templateRow.Remove();
// save changed document to a XLSX file
editor.Save(outFilename);
// export changed document to a PDF document
editor.Export(outPdfFilename);
}
}
Public Shared Sub XlsxCopyTableRowExample()
Dim templateFilename As String = "CopyTableRow_template.xlsx"
Dim outFilename As String = "CopyTableRow.xlsx"
Dim outPdfFilename As String = "CopyTableRow.pdf"
' create XlsxDocumentEditor that allows to edit file "CopyTableRow_template.xlsx"
Using editor As New Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor(templateFilename)
' get first sheet in XLSX document
Dim sheet As Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheet = editor.Sheets(0)
' find row that contains text "[cell1]"
Dim templateRow As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableRow = sheet.FindRow("[cell1]")
' create an array that contains colors
Dim colors As System.Drawing.Color() = New System.Drawing.Color() {System.Drawing.Color.Red, System.Drawing.Color.Green, System.Drawing.Color.Blue, System.Drawing.Color.Orange, System.Drawing.Color.Yellow}
' for each color
For i As Integer = 0 To colors.Length - 1
' insert copy of template row before template row
Dim rowCopy As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableRow = DirectCast(templateRow.InsertCopyBeforeSelf(), Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableRow)
' set row data
rowCopy(0).Text = String.Format("Copy {0} ({1})", i, colors(i))
rowCopy("[cell1]") = String.Format("cell data {0}", i)
' set cell colors
rowCopy(1).SetFillColor(colors(i))
rowCopy(2).SetFillColor(colors(i))
' if color has odd index in colors array
If i Mod 2 = 1 Then
' set row height to 10mm
rowCopy.Height = Vintasoft.Imaging.Utils.UnitOfMeasureConverter.ConvertToPoints(10, Vintasoft.Imaging.UnitOfMeasure.Millimeters)
End If
Next
' remove template row
templateRow.Remove()
' save changed document to a XLSX file
editor.Save(outFilename)
' export changed document to a PDF document
editor.Export(outPdfFilename)
End Using
End Sub
Вот C#/VB.NET код, который демонстрирует, как установить границы таблицы как в шаблонной ячейки таблицы (в примере используется документ-шаблон
SetTableBorders_template.xlsx
):
public static void XlsxSetTableBordersExample()
{
string templateFilename = "SetTableBorders_template.xlsx";
string outFilename = "SetTableBorders.xlsx";
string outPdfFilename = "SetTableBorders.pdf";
// create XlsxDocumentEditor that allows to edit file "SetTableBorders_template.xlsx"
using (Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor editor =
new Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor(templateFilename))
{
// get first sheet in XLSX document
Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheet sheet = editor.Sheets[0];
// get cell border templates
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableCell boldBorderTemplate =
sheet.FindCell("[bold]");
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableCell colorBorderTemplate =
sheet.FindCell("[color]");
// set borders from template cells
sheet.FindCell("[bold_cell]").SetBorder(boldBorderTemplate);
sheet.FindCell("[color_cell]").SetBorder(colorBorderTemplate);
// set outside border inside table
sheet.SetOutsideBorder(sheet.FindCell("[color_first]"), sheet.FindCell("[color_last]"), colorBorderTemplate);
sheet.SetOutsideBorder(sheet.FindCell("[bold_first]"), sheet.FindCell("[bold_last]"), boldBorderTemplate);
// remove border template table
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement removingRow = boldBorderTemplate.Row.Prev;
for (int i = 0; i < 5; i++)
{
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement row = removingRow;
removingRow = removingRow.Next;
row.Remove();
}
// save changed document to a XLSX file
editor.Save(outFilename);
// export changed document to a PDF document
editor.Export(outPdfFilename);
}
}
Public Shared Sub XlsxSetTableBordersExample()
Dim templateFilename As String = "SetTableBorders_template.xlsx"
Dim outFilename As String = "SetTableBorders.xlsx"
Dim outPdfFilename As String = "SetTableBorders.pdf"
' create XlsxDocumentEditor that allows to edit file "SetTableBorders_template.xlsx"
Using editor As New Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor(templateFilename)
' get first sheet in XLSX document
Dim sheet As Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheet = editor.Sheets(0)
' get cell border templates
Dim boldBorderTemplate As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableCell = sheet.FindCell("[bold]")
Dim colorBorderTemplate As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentTableCell = sheet.FindCell("[color]")
' set borders from template cells
sheet.FindCell("[bold_cell]").SetBorder(boldBorderTemplate)
sheet.FindCell("[color_cell]").SetBorder(colorBorderTemplate)
' set outside border inside table
sheet.SetOutsideBorder(sheet.FindCell("[color_first]"), sheet.FindCell("[color_last]"), colorBorderTemplate)
sheet.SetOutsideBorder(sheet.FindCell("[bold_first]"), sheet.FindCell("[bold_last]"), boldBorderTemplate)
' remove border template table
Dim removingRow As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement = boldBorderTemplate.Row.Prev
For i As Integer = 0 To 4
Dim row As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement = removingRow
removingRow = removingRow.[Next]
row.Remove()
Next
' save changed document to a XLSX file
editor.Save(outFilename)
' export changed document to a PDF document
editor.Export(outPdfFilename)
End Using
End Sub
Класс XlsxDocumentSheetCell
XlsxDocumentSheetCell представляет ячейку листа XLSX и позволяет:
Вот C#/VB.NET код, который демонстрирует, как найти ячейку по ссылке и установить формулу ячейки (пример использует шаблон документа
XlsxCell_template.xlsx
):
public static void XlsxCellExample()
{
string templateFilename = "XlsxCell_template.xlsx";
string outFilename = "XlsxCell.xlsx";
string outPdfFilename = "XlsxCell.pdf";
// create XlsxDocumentEditor that allows to edit file "SetBarcodeImage_template.xlsx"
using (Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor editor =
new Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor(templateFilename))
{
// get first sheet in XLSX document
Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheet sheet = editor.Sheets[0];
// find cell by reference (A1 format) and set cell fill color
sheet.FindCellByName("B3").SetFillColor(System.Drawing.Color.Red);
sheet.FindCellByName("D3").SetFillColor(System.Drawing.Color.Yellow);
sheet.FindCellByName("C4").SetFillColor(System.Drawing.Color.Green);
sheet.FindCellByName("D5").SetFillColor(System.Drawing.Color.Blue);
sheet.FindCellByName("B5").SetFillColor(System.Drawing.Color.Pink);
// set cell formula
Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheetCell cellD16 = sheet.FindCellByName("D8");
cellD16.Formula = "SUM(B8,C8)";
cellD16.Number = sheet.FindCellByName("B8").Number + sheet.FindCellByName("C8").Number;
// find cell that contains text "[cell_name]"
Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheetCell cell = sheet.FindCell("[cell_name]");
// set cell text to cell name in A1 format
cell.Text = cell.Name;
// save changed document to a XLSX file
editor.Save(outFilename);
// export changed document to a PDF document
editor.Export(outPdfFilename);
}
}
Public Shared Sub XlsxCellExample()
Dim templateFilename As String = "XlsxCell_template.xlsx"
Dim outFilename As String = "XlsxCell.xlsx"
Dim outPdfFilename As String = "XlsxCell.pdf"
' create XlsxDocumentEditor that allows to edit file "SetBarcodeImage_template.xlsx"
Using editor As New Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor(templateFilename)
' get first sheet in XLSX document
Dim sheet As Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheet = editor.Sheets(0)
' find cell by reference (A1 format) and set cell fill color
sheet.FindCellByName("B3").SetFillColor(System.Drawing.Color.Red)
sheet.FindCellByName("D3").SetFillColor(System.Drawing.Color.Yellow)
sheet.FindCellByName("C4").SetFillColor(System.Drawing.Color.Green)
sheet.FindCellByName("D5").SetFillColor(System.Drawing.Color.Blue)
sheet.FindCellByName("B5").SetFillColor(System.Drawing.Color.Pink)
' set cell formula
Dim cellD16 As Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheetCell = sheet.FindCellByName("D8")
cellD16.Formula = "SUM(B8,C8)"
cellD16.Number = sheet.FindCellByName("B8").Number + sheet.FindCellByName("C8").Number
' find cell that contains text "[cell_name]"
Dim cell As Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheetCell = sheet.FindCell("[cell_name]")
' set cell text to cell name in A1 format
cell.Text = cell.Name
' save changed document to a XLSX file
editor.Save(outFilename)
' export changed document to a PDF document
editor.Export(outPdfFilename)
End Using
End Sub
Класс OpenXmlDocumentImage
OpenXmlDocumentImage представляет изображение DOCX или XLSX документа:
Вот C#/VB.NET код, который демонстрирует, как установить изображение штрих-кода в XLSX документе (в примере используется документ-шаблон
SetBarcodeImage_template.xlsx
):
public static void XlsxSetBarcodeImageExample()
{
string templateFilename = "SetBarcodeImage_template.xlsx";
string outFilename = "SetBarcodeImage.xlsx";
string outPdfFilename = "SetBarcodeImage.pdf";
// create XlsxDocumentEditor that allows to edit file "SetBarcodeImage_template.xlsx"
using (Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor editor =
new Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor(templateFilename))
{
// get first sheet in XLSX document
Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheet sheet = editor.Sheets[0];
// find cell with text "Invoice number:"
Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheetCell cell = sheet.FindCell("Invoice number:");
// get barcode value as text of next row after "Invoice number:" cell
string barcodeValue = sheet.FindCell(cell.ColumnNumber, cell.RowNumber + 1).Text;
// create and setup barcode generator
Vintasoft.Barcode.BarcodeWriter barcodeGenerator = new Vintasoft.Barcode.BarcodeWriter();
barcodeGenerator.Settings.Barcode = Vintasoft.Barcode.BarcodeType.QR;
barcodeGenerator.Settings.SetWidth(200);
barcodeGenerator.Settings.Value = barcodeValue;
// generate barcode image
using (Vintasoft.Imaging.VintasoftImage barcodeImage =
new Vintasoft.Imaging.VintasoftImage(barcodeGenerator.GetBarcodeAsVintasoftBitmap(), true))
{
// crop barcode to the rectangular image
barcodeImage.Crop(new System.Drawing.Rectangle(0, 0, barcodeImage.Width, barcodeImage.Width));
// set barcode image to the DOCX image at index 0
editor.Images[0].SetImage(barcodeImage);
}
// save changed document to a XLSX file
editor.Save(outFilename);
// export changed document to a PDF document
editor.Export(outPdfFilename);
}
}
Public Shared Sub XlsxSetBarcodeImageExample()
Dim templateFilename As String = "SetBarcodeImage_template.xlsx"
Dim outFilename As String = "SetBarcodeImage.xlsx"
Dim outPdfFilename As String = "SetBarcodeImage.pdf"
' create XlsxDocumentEditor that allows to edit file "SetBarcodeImage_template.xlsx"
Using editor As New Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentEditor(templateFilename)
' get first sheet in XLSX document
Dim sheet As Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheet = editor.Sheets(0)
' find cell with text "Invoice number:"
Dim cell As Vintasoft.Imaging.Office.OpenXml.Editor.Xlsx.XlsxDocumentSheetCell = sheet.FindCell("Invoice number:")
' get barcode value as text of next row after "Invoice number:" cell
Dim barcodeValue As String = sheet.FindCell(cell.ColumnNumber, cell.RowNumber + 1).Text
' create and setup barcode generator
Dim barcodeGenerator As New Vintasoft.Barcode.BarcodeWriter()
barcodeGenerator.Settings.Barcode = Vintasoft.Barcode.BarcodeType.QR
barcodeGenerator.Settings.SetWidth(200)
barcodeGenerator.Settings.Value = barcodeValue
' generate barcode image
Using barcodeImage As New Vintasoft.Imaging.VintasoftImage(barcodeGenerator.GetBarcodeAsVintasoftBitmap(), True)
' crop barcode to the rectangular image
barcodeImage.Crop(New System.Drawing.Rectangle(0, 0, barcodeImage.Width, barcodeImage.Width))
' set barcode image to the DOCX image at index 0
editor.Images(0).SetImage(barcodeImage)
End Using
' save changed document to a XLSX file
editor.Save(outFilename)
' export changed document to a PDF document
editor.Export(outPdfFilename)
End Using
End Sub
Класс OpenXmlDocumentChart
OpenXmlDocumentChart представляет диаграмму DOCX или XLSX документа: