void BuildStyle_Font(HSSFWorkbook workbook, StringBuilder style, HSSFFont font) { switch (font.Boldweight) { case (short)FontBoldWeight.BOLD: style.Append("font-weight: bold; "); break; case (short)FontBoldWeight.NORMAL: // by default, not not increase HTML size // style.Append( "font-weight: normal; " ); break; } HSSFColor fontColor = workbook.GetCustomPalette().GetColor(font.Color); if (fontColor != null) { style.Append("color: " + ExcelToHtmlUtils.GetColor(fontColor) + "; "); } if (font.FontHeightInPoints != 0) { style.Append("font-size: " + font.FontHeightInPoints + "pt; "); } if (font.IsItalic) { style.Append("font-style: italic; "); } }
public static XmlDocument Process(string xlsFile) { HSSFWorkbook workbook = ExcelToHtmlUtils.LoadXls(xlsFile); ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(); excelToHtmlConverter.ProcessWorkbook(workbook); return(excelToHtmlConverter.Document); }
protected String BuildStyle(HSSFWorkbook workbook, HSSFCellStyle cellStyle) { StringBuilder style = new StringBuilder(); HSSFPalette palette = workbook.GetCustomPalette(); style.Append("white-space: pre-wrap; "); ExcelToHtmlUtils.AppendAlign(style, cellStyle.Alignment); if (cellStyle.FillPattern == FillPatternType.NO_FILL) { // no fill } else if (cellStyle.FillPattern == FillPatternType.SOLID_FOREGROUND) { //cellStyle. //HSSFColor. HSSFColor foregroundColor = palette.GetColor(cellStyle.FillForegroundColor); if (foregroundColor != null) { style.Append("background-color: " + ExcelToHtmlUtils.GetColor(foregroundColor) + "; "); } } else { HSSFColor backgroundColor = palette.GetColor(cellStyle.FillBackgroundColor); if (backgroundColor != null) { style.Append("background-color: " + ExcelToHtmlUtils.GetColor(backgroundColor) + "; "); } } BuildStyle_Border(workbook, style, "top", cellStyle.BorderTop, cellStyle.TopBorderColor); BuildStyle_Border(workbook, style, "right", cellStyle.BorderRight, cellStyle.RightBorderColor); BuildStyle_Border(workbook, style, "bottom", cellStyle.BorderBottom, cellStyle.BottomBorderColor); BuildStyle_Border(workbook, style, "left", cellStyle.BorderLeft, cellStyle.LeftBorderColor); HSSFFont font = cellStyle.GetFont(workbook) as HSSFFont; BuildStyle_Font(workbook, style, font); return(style.ToString()); }
private void BuildStyle_Border(HSSFWorkbook workbook, StringBuilder style, String type, BorderStyle xlsBorder, short borderColor) { if (xlsBorder == BorderStyle.NONE) { return; } StringBuilder borderStyle = new StringBuilder(); borderStyle.Append(ExcelToHtmlUtils.GetBorderWidth(xlsBorder)); borderStyle.Append(' '); borderStyle.Append(ExcelToHtmlUtils.GetBorderStyle(xlsBorder)); HSSFColor color = workbook.GetCustomPalette().GetColor(borderColor); if (color != null) { borderStyle.Append(' '); borderStyle.Append(ExcelToHtmlUtils.GetColor(color)); } style.Append("border-" + type + ": " + borderStyle + "; "); }
protected bool ProcessCell(HSSFCell cell, XmlElement tableCellElement, int normalWidthPx, int maxSpannedWidthPx, float normalHeightPt) { HSSFCellStyle cellStyle = cell.CellStyle as HSSFCellStyle; string value; switch (cell.CellType) { case CellType.STRING: // XXX: enrich value = cell.RichStringCellValue.String; break; case CellType.FORMULA: switch (cell.CachedFormulaResultType) { case CellType.STRING: HSSFRichTextString str = cell.RichStringCellValue as HSSFRichTextString; if (str != null && str.Length > 0) { value = (str.String); } else { value = string.Empty; } break; case CellType.NUMERIC: HSSFCellStyle style = cellStyle; if (style == null) { value = cell.NumericCellValue.ToString(); } else { value = (_formatter.FormatRawCellContents(cell.NumericCellValue, style.DataFormat, style.GetDataFormatString())); } break; case CellType.BOOLEAN: value = cell.BooleanCellValue.ToString(); break; case CellType.ERROR: value = ErrorEval.GetText(cell.ErrorCellValue); break; default: logger.Log(POILogger.WARN, "Unexpected cell cachedFormulaResultType (" + cell.CachedFormulaResultType.ToString() + ")"); value = string.Empty; break; } break; case CellType.BLANK: value = string.Empty; break; case CellType.NUMERIC: value = _formatter.FormatCellValue(cell); break; case CellType.BOOLEAN: value = cell.BooleanCellValue.ToString(); break; case CellType.ERROR: value = ErrorEval.GetText(cell.ErrorCellValue); break; default: logger.Log(POILogger.WARN, "Unexpected cell type (" + cell.CellType.ToString() + ")"); return(true); } bool noText = string.IsNullOrEmpty(value); bool wrapInDivs = !noText && UseDivsToSpan && !cellStyle.WrapText; short cellStyleIndex = cellStyle.Index; if (cellStyleIndex != 0) { HSSFWorkbook workbook = cell.Row.Sheet.Workbook as HSSFWorkbook; string mainCssClass = GetStyleClassName(workbook, cellStyle); if (wrapInDivs) { tableCellElement.SetAttribute("class", mainCssClass + " " + cssClassContainerCell); } else { tableCellElement.SetAttribute("class", mainCssClass); } if (noText) { /* * if cell style is defined (like borders, etc.) but cell text * is empty, add " " to output, so browser won't collapse * and ignore cell */ value = "\u00A0"; //“ ”全角空格 } } if (OutputLeadingSpacesAsNonBreaking && value.StartsWith(" ")) { StringBuilder builder = new StringBuilder(); for (int c = 0; c < value.Length; c++) { if (value[c] != ' ') { break; } builder.Append('\u00a0'); } if (value.Length != builder.Length) { builder.Append(value.Substring(builder.Length)); } value = builder.ToString(); } XmlText text = htmlDocumentFacade.CreateText(value); if (wrapInDivs) { XmlElement outerDiv = htmlDocumentFacade.CreateBlock(); outerDiv.SetAttribute("class", this.cssClassContainerDiv); XmlElement innerDiv = htmlDocumentFacade.CreateBlock(); StringBuilder innerDivStyle = new StringBuilder(); innerDivStyle.Append("position:absolute;min-width:"); innerDivStyle.Append(normalWidthPx); innerDivStyle.Append("px;"); if (maxSpannedWidthPx != int.MaxValue) { innerDivStyle.Append("max-width:"); innerDivStyle.Append(maxSpannedWidthPx); innerDivStyle.Append("px;"); } innerDivStyle.Append("overflow:hidden;max-height:"); innerDivStyle.Append(normalHeightPt); innerDivStyle.Append("pt;white-space:nowrap;"); ExcelToHtmlUtils.AppendAlign(innerDivStyle, cellStyle.Alignment); htmlDocumentFacade.AddStyleClass(outerDiv, "d", innerDivStyle.ToString()); innerDiv.AppendChild(text); outerDiv.AppendChild(innerDiv); tableCellElement.AppendChild(outerDiv); } else { tableCellElement.AppendChild(text); } return(string.IsNullOrEmpty(value) && cellStyleIndex == 0); }
protected static int GetColumnWidth(HSSFSheet sheet, int columnIndex) { return(ExcelToHtmlUtils.GetColumnWidthInPx(sheet.GetColumnWidth(columnIndex))); }
/** * @return maximum 1-base index of column that were rendered, zero if none */ protected int ProcessRow(CellRangeAddress[][] mergedRanges, HSSFRow row, XmlElement tableRowElement) { HSSFSheet sheet = (HSSFSheet)row.Sheet; int maxColIx = row.LastCellNum; if (maxColIx <= 0) { return(0); } List <XmlElement> emptyCells = new List <XmlElement>(maxColIx); if (OutputRowNumbers) { XmlElement tableRowNumberCellElement = htmlDocumentFacade.CreateTableHeaderCell(); ProcessRowNumber(row, tableRowNumberCellElement); emptyCells.Add(tableRowNumberCellElement); } int maxRenderedColumn = 0; for (int colIx = 0; colIx < maxColIx; colIx++) { if (!OutputHiddenColumns && sheet.IsColumnHidden(colIx)) { continue; } CellRangeAddress range = ExcelToHtmlUtils.GetMergedRange( mergedRanges, row.RowNum, colIx); if (range != null && (range.FirstColumn != colIx || range.FirstRow != row.RowNum)) { continue; } HSSFCell cell = (HSSFCell)row.GetCell(colIx); int divWidthPx = 0; if (UseDivsToSpan) { divWidthPx = GetColumnWidth(sheet, colIx); bool hasBreaks = false; for (int nextColumnIndex = colIx + 1; nextColumnIndex < maxColIx; nextColumnIndex++) { if (!OutputHiddenColumns && sheet.IsColumnHidden(nextColumnIndex)) { continue; } if (row.GetCell(nextColumnIndex) != null && !IsTextEmpty((HSSFCell)row.GetCell(nextColumnIndex))) { hasBreaks = true; break; } divWidthPx += GetColumnWidth(sheet, nextColumnIndex); } if (!hasBreaks) { divWidthPx = int.MaxValue; } } XmlElement tableCellElement = htmlDocumentFacade.CreateTableCell(); if (range != null) { if (range.FirstColumn != range.LastColumn) { tableCellElement.SetAttribute("colspan", (range.LastColumn - range.FirstColumn + 1).ToString()); } if (range.FirstRow != range.LastRow) { tableCellElement.SetAttribute("rowspan", (range.LastRow - range.FirstRow + 1).ToString()); } } bool emptyCell; if (cell != null) { emptyCell = ProcessCell(cell, tableCellElement, GetColumnWidth(sheet, colIx), divWidthPx, row.Height / 20f); } else { emptyCell = true; } if (emptyCell) { emptyCells.Add(tableCellElement); } else { foreach (XmlElement emptyCellElement in emptyCells) { tableRowElement.AppendChild(emptyCellElement); } emptyCells.Clear(); tableRowElement.AppendChild(tableCellElement); maxRenderedColumn = colIx; } } return(maxRenderedColumn + 1); }
protected void ProcessSheet(HSSFSheet sheet) { ProcessSheetHeader(htmlDocumentFacade.Body, sheet); int physicalNumberOfRows = sheet.PhysicalNumberOfRows; if (physicalNumberOfRows <= 0) { return; } XmlElement table = htmlDocumentFacade.CreateTable(); table.SetAttribute("class", cssClassTable); XmlElement tableBody = htmlDocumentFacade.CreateTableBody(); CellRangeAddress[][] mergedRanges = ExcelToHtmlUtils.BuildMergedRangesMap(sheet); List <XmlElement> emptyRowElements = new List <XmlElement>(physicalNumberOfRows); int maxSheetColumns = 1; for (int r = 0; r < physicalNumberOfRows; r++) { HSSFRow row = (HSSFRow)sheet.GetRow(r); if (row == null) { continue; } if (!OutputHiddenRows && row.ZeroHeight) { continue; } XmlElement tableRowElement = htmlDocumentFacade.CreateTableRow(); htmlDocumentFacade.AddStyleClass(tableRowElement, "r", "height:" + (row.Height / 20f) + "pt;"); int maxRowColumnNumber = ProcessRow(mergedRanges, row, tableRowElement); if (maxRowColumnNumber == 0) { emptyRowElements.Add(tableRowElement); } else { if (emptyRowElements.Count > 0) { foreach (XmlElement emptyRowElement in emptyRowElements) { tableBody.AppendChild(emptyRowElement); } emptyRowElements.Clear(); } tableBody.AppendChild(tableRowElement); } maxSheetColumns = Math.Max(maxSheetColumns, maxRowColumnNumber); } ProcessColumnWidths(sheet, maxSheetColumns, table); if (OutputColumnHeaders) { ProcessColumnHeaders(sheet, maxSheetColumns, table); } table.AppendChild(tableBody); htmlDocumentFacade.Body.AppendChild(table); }