public void SetRowStyle(Sheet sheet, int rowIndex) { HSSFRow targetRow = null; HSSFCell sourceCell = null; HSSFCell targetCell = null; HSSFRow sourceRow = null; sourceRow = (HSSFRow)(sheet.GetRow(rowIndex - 1)); targetRow = (HSSFRow)(sheet.GetRow(rowIndex)); for (int m = sourceRow.FirstCellNum; m < sourceRow.LastCellNum; m++) { sourceCell = (HSSFCell)(sourceRow.GetCell(m)); if (sourceCell == null) { continue; } if (targetRow.GetCell(m) == null) { targetRow.CreateCell(m); } targetCell = (HSSFCell)(targetRow.GetCell(m)); targetCell.Encoding = sourceCell.Encoding; CellStyle cellstyle = sourceCell.CellStyle; if (cellstyle == null) { cellstyle = sheet.Workbook.CreateCellStyle(); } cellstyle.CloneStyleFrom(sourceCell.CellStyle); targetCell.CellStyle = cellstyle; if (!string.IsNullOrWhiteSpace(sourceCell.CellFormula)) { targetCell.CellFormula = sourceCell.CellFormula.Replace(rowIndex.ToString(), (rowIndex + 1).ToString()); } if (sourceCell.CellType == CellType.NUMERIC || sourceCell.CellType == CellType.BOOLEAN || sourceCell.CellType == CellType.STRING) { targetCell.SetCellType(sourceCell.CellType); } } }
/* * 设置单元格样式 * * Param pageIndex 页码 * Param rowIndexRelative 相对行号 * Param cellIndex 列号 * Param fontName 字体名称 * Param fontSize 字体大小 * * Return 生成文件的URL */ protected void SetRowCellStyle(int pageIndex, int rowIndexRelative, int cellIndex, String fontName, short fontSize) { int rowIndexAbsolute = this.GetRowIndexAbsolute(pageIndex, rowIndexRelative); Row row = this.GetRow(rowIndexAbsolute); Cell cell = row.GetCell((short)cellIndex); if (cell == null) { cell = row.CreateCell((short)cellIndex); } CellStyle style = workbook.CreateCellStyle(); if (cell.CellStyle != null) { style.CloneStyleFrom(cell.CellStyle); } Font font = workbook.CreateFont(); font.FontName = fontName; font.FontHeightInPoints = fontSize; style.SetFont(font); cell.CellStyle = workbook.CreateCellStyle(); cell.CellStyle.CloneStyleFrom(style); }