Created
December 5, 2015 10:46
-
-
Save dodbrian/15dbeb58c6c0c168422f to your computer and use it in GitHub Desktop.
Using .xlsx file as a report template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using OfficeOpenXml; | |
using SpreadsheetLight; | |
namespace XlsxTemplateTest | |
{ | |
internal static class Program | |
{ | |
private static void Main() | |
{ | |
SpreadsheetLightExport(); | |
} | |
private static void SpreadsheetLightExport() | |
{ | |
using (var sl = new SLDocument("TTNTemplate.xlsx")) | |
{ | |
// Найти ячейку, содержащую номер накладной | |
var cellNumber = | |
sl.GetCells() | |
.FirstOrDefault( | |
c => sl.GetCellValueAsString(c.Key.RowIndex, c.Key.ColumnIndex) == "{{НомерНакладной}}"); | |
// Установить значение номера накладной | |
sl.SetCellValue(cellNumber.Key.RowIndex, cellNumber.Key.ColumnIndex, 500); | |
// Найти ячейку, содержащую дату накладной | |
var cellDate = | |
sl.GetCells() | |
.FirstOrDefault( | |
c => sl.GetCellValueAsString(c.Key.RowIndex, c.Key.ColumnIndex) == "{{ДатаСоставления}}"); | |
// Установить значение даты накладной | |
sl.SetCellValue(cellDate.Key.RowIndex, cellDate.Key.ColumnIndex, DateTime.Now.ToShortDateString()); | |
// Найти именованный ряд, содержащий позиции накладной | |
var positionRow = Convert.ToInt16(sl.GetDefinedNames() | |
.FirstOrDefault(x => x.Name == "Positions")? | |
.Text.Split(':') | |
.Last() | |
.Replace("$", "")); | |
var rowHeight = sl.GetRowHeight(positionRow); | |
const int numRows = 3; | |
sl.InsertRow(positionRow + 1, numRows); | |
for (var i = 1; i <= numRows; i++) | |
{ | |
sl.CopyRow(positionRow, positionRow + i); | |
sl.SetRowHeight(positionRow + i, rowHeight); | |
} | |
sl.SaveAs("TTNOut.xlsx"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment