本文整理汇总了C#中ISheet.GetMergedRegion方法的典型用法代码示例。如果您正苦于以下问题:C# ISheet.GetMergedRegion方法的具体用法?C# ISheet.GetMergedRegion怎么用?C# ISheet.GetMergedRegion使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ISheet
的用法示例。
在下文中一共展示了ISheet.GetMergedRegion方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BuildMergedRangesMap
/**
* Creates a map (i.e. two-dimensional array) filled with ranges. Allow fast
* retrieving {@link CellRangeAddress} of any cell, if cell is contained in
* range.
*
* @see #getMergedRange(CellRangeAddress[][], int, int)
*/
public static CellRangeAddress[][] BuildMergedRangesMap(ISheet sheet)
{
CellRangeAddress[][] mergedRanges = new CellRangeAddress[1][];
for ( int m = 0; m < sheet.NumMergedRegions; m++ )
{
CellRangeAddress cellRangeAddress = sheet.GetMergedRegion( m );
int requiredHeight = cellRangeAddress.LastRow + 1;
if ( mergedRanges.Length < requiredHeight )
{
CellRangeAddress[][] newArray = new CellRangeAddress[requiredHeight][];
Array.Copy( mergedRanges, 0, newArray, 0, mergedRanges.Length );
mergedRanges = newArray;
}
for ( int r = cellRangeAddress.FirstRow; r <= cellRangeAddress.LastRow; r++ )
{
int requiredWidth = cellRangeAddress.LastColumn + 1;
CellRangeAddress[] rowMerged = mergedRanges[r];
if ( rowMerged == null )
{
rowMerged = new CellRangeAddress[requiredWidth];
mergedRanges[r] = rowMerged;
}
else
{
int rowMergedLength = rowMerged.Length;
if ( rowMergedLength < requiredWidth )
{
CellRangeAddress[] newRow = new CellRangeAddress[requiredWidth];
Array.Copy(rowMerged, 0, newRow, 0,rowMergedLength );
mergedRanges[r] = newRow;
rowMerged = newRow;
}
}
//Arrays.Fill( rowMerged, cellRangeAddress.FirstColumn, cellRangeAddress.LastColumn + 1, cellRangeAddress );
for (int i = cellRangeAddress.FirstColumn; i < cellRangeAddress.LastColumn + 1; i++)
{
rowMerged[i] = cellRangeAddress;
}
}
}
return mergedRanges;
}
示例2: CopyRow
private void CopyRow(HSSFWorkbook workbook, ISheet sourceWorksheet, ISheet destinationWorksheet, int sourceRowNum, int destinationRowNum)
{
// Get the source / new row
IRow destinationRow;
var getRows = destinationWorksheet.GetRow(destinationRowNum);
if (getRows == null)
{
destinationRow = destinationWorksheet.CreateRow(destinationRowNum);
}
else
{
destinationRow = getRows;
}
IRow sourceRow = sourceWorksheet.GetRow(sourceRowNum);
// Loop through source columns to add to new row
for (int i = 0; i < sourceRow.LastCellNum; i++)
{
// Grab a copy of the old/new cell
ICell oldCell = sourceRow.GetCell(i);
ICell newCell = destinationRow.CreateCell(i);
// If the old cell is null jump to next cell
if (oldCell == null)
{
newCell = null;
continue;
}
// Copy style from old cell and apply to new cell
ICellStyle newCellStyle = workbook.CreateCellStyle();
newCellStyle.CloneStyleFrom(oldCell.CellStyle); ;
newCell.CellStyle = newCellStyle;
// If there is a cell comment, copy
if (newCell.CellComment != null) newCell.CellComment = oldCell.CellComment;
// If there is a cell hyperlink, copy
if (oldCell.Hyperlink != null) newCell.Hyperlink = oldCell.Hyperlink;
// Set the cell data type
newCell.SetCellType(oldCell.CellType);
// Set the cell data value
switch (oldCell.CellType)
{
case CellType.Blank:
newCell.SetCellValue(oldCell.StringCellValue);
break;
case CellType.Boolean:
newCell.SetCellValue(oldCell.BooleanCellValue);
break;
case CellType.Error:
newCell.SetCellErrorValue(oldCell.ErrorCellValue);
break;
case CellType.Formula:
newCell.SetCellFormula(oldCell.CellFormula);
break;
case CellType.Numeric:
newCell.SetCellValue(oldCell.NumericCellValue);
break;
case CellType.String:
newCell.SetCellValue(oldCell.RichStringCellValue);
break;
case CellType.Unknown:
newCell.SetCellValue(oldCell.StringCellValue);
break;
}
}
// If there are are any merged regions in the source row, copy to new row
for (int i = 0; i < sourceWorksheet.NumMergedRegions; i++)
{
CellRangeAddress cellRangeAddress = sourceWorksheet.GetMergedRegion(i);
if (cellRangeAddress.FirstRow == sourceRow.RowNum)
{
CellRangeAddress newCellRangeAddress = new CellRangeAddress(destinationRow.RowNum,
destinationRow.RowNum + (cellRangeAddress.LastRow - cellRangeAddress.FirstRow),
cellRangeAddress.FirstColumn,
cellRangeAddress.LastColumn);
destinationWorksheet.AddMergedRegion(newCellRangeAddress);
}
}
}
示例3: CopyRow
public static IRow CopyRow(ISheet sheet, int sourceRowIndex, int targetRowIndex)
{
if (sourceRowIndex == targetRowIndex)
throw new ArgumentException("sourceIndex and targetIndex cannot be same");
// Get the source / new row
IRow newRow = sheet.GetRow(targetRowIndex);
IRow sourceRow = sheet.GetRow(sourceRowIndex);
// If the row exist in destination, push down all rows by 1 else create a new row
if (newRow != null)
{
sheet.ShiftRows(targetRowIndex, sheet.LastRowNum, 1);
}
else
{
newRow = sheet.CreateRow(targetRowIndex);
}
// Loop through source columns to add to new row
for (int i = sourceRow.FirstCellNum; i < sourceRow.LastCellNum; i++)
{
// Grab a copy of the old/new cell
ICell oldCell = sourceRow.GetCell(i);
// If the old cell is null jump to next cell
if (oldCell == null)
{
continue;
}
ICell newCell = newRow.CreateCell(i);
if (oldCell.CellStyle != null)
{
// apply style from old cell to new cell
newCell.CellStyle = oldCell.CellStyle;
}
// If there is a cell comment, copy
if (oldCell.CellComment != null)
{
newCell.CellComment = oldCell.CellComment;
}
// If there is a cell hyperlink, copy
if (oldCell.Hyperlink != null)
{
newCell.Hyperlink = oldCell.Hyperlink;
}
// Set the cell data type
newCell.SetCellType(oldCell.CellType);
// Set the cell data value
switch (oldCell.CellType)
{
case CellType.Blank:
newCell.SetCellValue(oldCell.StringCellValue);
break;
case CellType.Boolean:
newCell.SetCellValue(oldCell.BooleanCellValue);
break;
case CellType.Error:
newCell.SetCellErrorValue(oldCell.ErrorCellValue);
break;
case CellType.Formula:
newCell.SetCellFormula(oldCell.CellFormula);
break;
case CellType.Numeric:
newCell.SetCellValue(oldCell.NumericCellValue);
break;
case CellType.String:
newCell.SetCellValue(oldCell.RichStringCellValue);
break;
}
}
// If there are are any merged regions in the source row, copy to new row
for (int i = 0; i < sheet.NumMergedRegions; i++)
{
CellRangeAddress cellRangeAddress = sheet.GetMergedRegion(i);
if (cellRangeAddress.FirstRow == sourceRow.RowNum)
{
CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.RowNum,
(newRow.RowNum +
(cellRangeAddress.LastRow - cellRangeAddress.FirstRow
)),
cellRangeAddress.FirstColumn,
cellRangeAddress.LastColumn);
sheet.AddMergedRegion(newCellRangeAddress);
}
}
return newRow;
}
示例4: GetColumnWidth
// /**
// * Drawing context to measure text
// */
//private static FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
/**
* Compute width of a column and return the result
*
* @param sheet the sheet to calculate
* @param column 0-based index of the column
* @param useMergedCells whether to use merged cells
* @return the width in pixels
*/
public static double GetColumnWidth(ISheet sheet, int column, bool useMergedCells)
{
//AttributedString str;
//TextLayout layout;
IWorkbook wb = sheet.Workbook;
DataFormatter formatter = new DataFormatter();
IFont defaultFont = wb.GetFontAt((short)0);
//str = new AttributedString((defaultChar));
//copyAttributes(defaultFont, str, 0, 1);
//layout = new TextLayout(str.Iterator, fontRenderContext);
//int defaultCharWidth = (int)layout.Advance;
Font font = IFont2Font(defaultFont);
int defaultCharWidth = TextRenderer.MeasureText("" + new String(defaultChar, 1), font).Width;
DummyEvaluator dummyEvaluator = new DummyEvaluator();
double width = -1;
using (Bitmap bmp = new Bitmap(2048, 100))
{
Graphics g = Graphics.FromImage(bmp);
//rows:
bool skipthisrow = false;
for (IEnumerator it = sheet.GetRowEnumerator(); it.MoveNext(); )
{
IRow row = (IRow)it.Current;
ICell cell = row.GetCell(column);
if (cell == null)
{
continue;
}
int colspan = 1;
for (int i = 0; i < sheet.NumMergedRegions; i++)
{
CellRangeAddress region = sheet.GetMergedRegion(i);
if (ContainsCell(region, row.RowNum, column))
{
if (!useMergedCells)
{
// If we're not using merged cells, skip this one and Move on to the next.
//continue rows;
skipthisrow = true;
}
cell = row.GetCell(region.FirstColumn);
colspan = 1 + region.LastColumn - region.FirstColumn;
}
}
if (skipthisrow)
{
continue;
}
ICellStyle style = cell.CellStyle;
NPOI.SS.UserModel.IFont font1 = wb.GetFontAt(style.FontIndex);
CellType cellType = cell.CellType;
// for formula cells we compute the cell width for the cached formula result
if (cellType == CellType.FORMULA) cellType = cell.CachedFormulaResultType;
if (cellType == CellType.STRING)
{
IRichTextString rt = cell.RichStringCellValue;
String[] lines = rt.String.Split("\n".ToCharArray());
for (int i = 0; i < lines.Length; i++)
{
String txt = lines[i] + defaultChar;
//str = new AttributedString(txt);
//copyAttributes(font, str, 0, txt.Length);
font = IFont2Font(font1);
if (rt.NumFormattingRuns > 0)
{
// TODO: support rich text fragments
}
//layout = new TextLayout(str.Iterator, fontRenderContext);
if (style.Rotation != 0)
{
/*
* Transform the text using a scale so that it's height is increased by a multiple of the leading,
* and then rotate the text before computing the bounds. The scale results in some whitespace around
* the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but
* is Added by the standard Excel autosize.
*/
double angle = style.Rotation * 2.0 * Math.PI / 360.0;
//.........这里部分代码省略.........
示例5: GetCellWithMerges
/**
* Return the cell, taking account of merged regions. Allows you to find the
* cell who's contents are Shown in a given position in the sheet.
*
* <p>If the cell at the given co-ordinates is a merged cell, this will
* return the primary (top-left) most cell of the merged region.</p>
* <p>If the cell at the given co-ordinates is not in a merged region,
* then will return the cell itself.</p>
* <p>If there is no cell defined at the given co-ordinates, will return
* null.</p>
*/
public static ICell GetCellWithMerges(ISheet sheet, int rowIx, int colIx)
{
IRow r = sheet.GetRow(rowIx);
if (r != null)
{
ICell c = r.GetCell(colIx);
if (c != null)
{
// Normal, non-merged cell
return c;
}
}
for (int mr = 0; mr < sheet.NumMergedRegions; mr++)
{
CellRangeAddress mergedRegion = sheet.GetMergedRegion(mr);
if (mergedRegion.IsInRange(rowIx, colIx))
{
// The cell wanted is in this merged range
// Return the primary (top-left) cell for the range
r = sheet.GetRow(mergedRegion.FirstRow);
if (r != null)
{
return r.GetCell(mergedRegion.FirstColumn);
}
}
}
// If we Get here, then the cell isn't defined, and doesn't
// live within any merged regions
return null;
}
示例6: GetTdMergedInfo
/// <summary>
/// 获取Table某个TD合并的列数和行数等信息。与Excel中对应Cell的合并行数和列数一致。
/// </summary>
/// <param name="rowIndex">行号</param>
/// <param name="colIndex">列号</param>
/// <param name="colspan">TD中需要合并的行数</param>
/// <param name="rowspan">TD中需要合并的列数</param>
/// <param name="rowspan">此单元格是否被某个行合并包含在内。如果被包含在内,将不输出TD。</param>
/// <returns></returns>
private void GetTdMergedInfo(ISheet sheet, int rowIndex, int colIndex, out int colspan, out int rowspan, out bool isByRowMerged)
{
colspan = 1;
rowspan = 1;
isByRowMerged = false;
int regionsCuont = sheet.NumMergedRegions;
CellRangeAddress region;
for (int i = 0; i < regionsCuont; i++)
{
region = sheet.GetMergedRegion(i);
if (region.FirstRow == rowIndex && region.FirstColumn == colIndex)
{
colspan = region.LastColumn - region.FirstColumn + 1;
rowspan = region.LastRow - region.FirstRow + 1;
return;
}
else if (rowIndex > region.FirstRow && rowIndex <= region.LastRow && colIndex >= region.FirstColumn && colIndex <= region.LastColumn)
{
isByRowMerged = true;
}
}
}
示例7: AddSheetHeader
/// <summary>
/// 为Excel添加表头
/// </summary>
/// <param name="sheet"></param>
/// <param name="headerRow">GridView的HeaderRow属性</param>
/// <param name="headerCellStyle">表头格式</param>
/// <param name="flagNewLine">转行标志</param>
/// <param name="colCount">Excel表列数</param>
/// <returns>Excel表格行数</returns>
private int AddSheetHeader(ISheet sheet, GridViewRow headerRow, ICellStyle headerCellStyle, string flagNewLine, out int colCount)
{
//int
colCount = 0;//记录GridView列数
int rowInex = 0;//记录表头的行数
IRow row = sheet.CreateRow(0);
ICell cell;
int groupCount = 0;//记录分组数
int colIndex = 0;//记录列索引,并于结束表头遍历后记录总列数
for (int i = 0; i < headerRow.Cells.Count; i++)
{
if (rowInex != groupCount)//新增了标题行时重新创建
{
row = sheet.CreateRow(rowInex);
groupCount = rowInex;
}
#region 是否跳过当前单元格
for (int m = 0; m < sheet.NumMergedRegions; m++)//遍历所有合并区域
{
NPOI.SS.Util.CellRangeAddress a = sheet.GetMergedRegion(m);
//当前单元格是处于合并区域内
if (a.FirstColumn <= colIndex && a.LastColumn >= colIndex
&& a.FirstRow <= rowInex && a.LastRow >= rowInex)
{
colIndex++;
m = 0;//重新遍历所有合并区域判断新单元格是否位于合并区域
}
}
#endregion
cell = row.CreateCell(colIndex);
cell.CellStyle = headerCellStyle;
TableCell tablecell = headerRow.Cells[i];
//跨列属性可能为添加了html属性colspan,也可能是由cell的ColumnSpan属性指定
int colSpan = 0;
int rowSpan = 0;
#region 获取跨行跨列属性值
//跨列
if (!string.IsNullOrEmpty(tablecell.Attributes["colspan"]))
{
colSpan = int.Parse(tablecell.Attributes["colspan"].ToString());
colSpan--;
}
if (tablecell.ColumnSpan > 1)
{
colSpan = tablecell.ColumnSpan;
colSpan--;
}
//跨行
if (!string.IsNullOrEmpty(tablecell.Attributes["rowSpan"]))
{
rowSpan = int.Parse(tablecell.Attributes["rowSpan"].ToString());
rowSpan--;
}
if (tablecell.RowSpan > 1)
{
rowSpan = tablecell.RowSpan;
rowSpan--;
}
#endregion
//添加excel合并区域
if (colSpan > 0 || rowSpan > 0)
{
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowInex, rowInex + rowSpan, colIndex, colIndex + colSpan));
colIndex += colSpan + 1;//重新设置列索引
}
else
{
colIndex++;
}
string strHeader = headerRow.Cells[i].Text;
if (strHeader.Contains(flagNewLine))//换行标记,当只存在一行标题时不存在</th></tr><tr>,此时colCount无法被赋值
{
rowInex++;
colCount = colIndex;
colIndex = 0;
strHeader = strHeader.Substring(0, strHeader.IndexOf("</th></tr><tr>"));
}
cell.SetCellValue(strHeader);
//.........这里部分代码省略.........