Hi,
is there a snippet or function to duplicate a cell or a row ? (like adding a line in an invoice)
Thanks,
Vincent
sheet.InsertRow(newRow, 1, InsertOptionsType.FormatAsAfter);
Workbook book = new Workbook();
book.LoadFromFile( "facture 2 (defaut).xlsx", ExcelVersion.Version2010);
Worksheet sheet = book.Worksheets[0];
sheet.InsertRow(22, 1, InsertOptionsType.FormatAsBefore);
sheet.InsertRow(19, 1, InsertOptionsType.FormatAsBefore);
sheet.InsertRow(18, 1, InsertOptionsType.FormatAsBefore);
sheet.InsertRow(24, 1, InsertOptionsType.FormatAsBefore);
/// <summary>
/// Duplicate Excel row style
/// </summary>
/// <param name="sheet">Excel document.</param>
/// <param name="sourceLine">Source line to copy.</param>
/// <param name="destinationLine">Destination line for copy.</param>
/// <param name="maxColumns">Max columns to duplicate.</param>
/// <param name="version">The Excel version to use.</param>
public static void DuplicateRowStyle(Worksheet sheet, int sourceLine, int destinationLine, int maxColumns, ExcelVersion version)
{
// Limit max columns for speed
int max = maxColumns < ExcelRequirements.MaxColumns(version) ? maxColumns : ExcelRequirements.MaxColumns(version);
// Loop on each column to duplicate cells
int col = 1;
do
{
string sourceCellName = ExcelUtility.ColumnNameFromNumber(col) + sourceLine.ToString();
string destinationCellName = ExcelUtility.ColumnNameFromNumber(col) + destinationLine.ToString();
CellRange sourceCell = sheet.Range[sourceCellName];
CellRange destinationCell = sheet.Range[destinationCellName];
if (sourceCell.HasMerged == true)
{
Duplicate(sourceCell, destinationCell);
// Skip merge cells
int sourceMergeStart = col;
int sourceMergeEnd = col;
do
{
sourceMergeEnd++;
} while ((sourceMergeEnd < max) && (sheet.Range[ExcelUtility.ColumnNameFromNumber(sourceMergeEnd) + sourceLine.ToString()].HasMerged == true));
// Merge new area on destination line
string range = ExcelUtility.ColumnNameFromNumber(sourceMergeStart) + destinationLine.ToString() + ":" + ExcelUtility.ColumnNameFromNumber(sourceMergeEnd-1) + destinationLine.ToString();
sheet.Range[range].Merge();
col += (sourceMergeEnd - sourceMergeStart);
}
else
{
Duplicate(sourceCell, destinationCell);
col++;
}
} while (col < max);
}
/// <summary>
/// Duplicates the specified cell range attribute to another range.
/// </summary>
/// <param name="source">The source range.</param>
/// <param name="destination">The destination range.</param>
private static void Duplicate(CellRange source, CellRange destination)
{
destination.Style = source.Style;
destination.NumberFormat = source.NumberFormat;
destination.HorizontalAlignment = source.HorizontalAlignment;
destination.VerticalAlignment = source.VerticalAlignment;
destination.Borders.LineStyle = source.Borders.LineStyle;
destination.Borders[BordersLineType.DiagonalDown].LineStyle = source.Borders[BordersLineType.DiagonalDown].LineStyle;
destination.Borders[BordersLineType.DiagonalUp].LineStyle = source.Borders[BordersLineType.DiagonalUp].LineStyle;
destination.Borders[BordersLineType.EdgeBottom].LineStyle = source.Borders[BordersLineType.EdgeBottom].LineStyle;
destination.Borders[BordersLineType.EdgeLeft].LineStyle = source.Borders[BordersLineType.EdgeLeft].LineStyle;
destination.Borders[BordersLineType.EdgeRight].LineStyle = source.Borders[BordersLineType.EdgeRight].LineStyle;
destination.Borders[BordersLineType.EdgeTop].LineStyle = source.Borders[BordersLineType.EdgeTop].LineStyle;
destination.Borders.Color = source.Borders.Color;
destination.Borders.Value = source.Borders.Value;
}
static void Main(string[] args)
{
string strTemplate = Path.GetFullPath(@".\Templates\facture (defaut).xlsx");
string strDocument = Path.GetFullPath(@"result.xlsx");
using (Workbook workbook = new Workbook())
{
workbook.LoadFromFile(strTemplate, Program.CurrentExcelVersion);
// Gets worksheet
Worksheet sheet = ExcelUtility.GetWorkSheet(workbook, "Invoice");
sheet.InsertRow(22, 1, InsertOptionsType.FormatAsAfter);
workbook.SaveToFile(strDocument, Program.CurrentExcelVersion);
System.Diagnostics.Process.Start(strDocument);
}
return;
}