268 lines
7.8 KiB
C#
268 lines
7.8 KiB
C#
![]() |
using System;
|
|||
|
using System.IO;
|
|||
|
using System.Text;
|
|||
|
using CPF.Drawing;
|
|||
|
using CPF.ReoGrid.Common;
|
|||
|
using CPF.ReoGrid.Core;
|
|||
|
using CPF.ReoGrid.XML;
|
|||
|
|
|||
|
namespace CPF.ReoGrid.IO
|
|||
|
{
|
|||
|
internal class RGHTMLExporter
|
|||
|
{
|
|||
|
public static void Export(Stream s, Worksheet sheet, string pageTitle, bool htmlHeader = true)
|
|||
|
{
|
|||
|
using (StreamWriter streamWriter = new StreamWriter(s))
|
|||
|
{
|
|||
|
StringBuilder stringBuilder = new StringBuilder();
|
|||
|
if (htmlHeader)
|
|||
|
{
|
|||
|
streamWriter.WriteLine("<!DOCTYPE html>");
|
|||
|
streamWriter.WriteLine("<html>");
|
|||
|
streamWriter.WriteLine("<head>");
|
|||
|
streamWriter.WriteLine(" <title>{0}</title>", pageTitle);
|
|||
|
streamWriter.WriteLine(" <meta content=\"text/html; charset=UTF-8\">");
|
|||
|
streamWriter.WriteLine("</head>");
|
|||
|
streamWriter.WriteLine("<body>");
|
|||
|
}
|
|||
|
streamWriter.WriteLine(" <table style='border-collapse:collapse;border:none;'>");
|
|||
|
int maxContentRow = sheet.MaxContentRow;
|
|||
|
int maxContentCol = sheet.MaxContentCol;
|
|||
|
for (int i = 0; i <= maxContentRow; i++)
|
|||
|
{
|
|||
|
RowHeader rowHeader = sheet.RetrieveRowHeader(i);
|
|||
|
streamWriter.WriteLine(string.Format(" <tr style='height:{0}px;'>", rowHeader.InnerHeight));
|
|||
|
int j = 0;
|
|||
|
while (j <= maxContentCol)
|
|||
|
{
|
|||
|
ColumnHeader columnHeader = sheet.RetrieveColumnHeader(j);
|
|||
|
Cell cell = sheet.GetCell(i, j);
|
|||
|
bool flag = cell != null && (cell.Colspan <= 0 || cell.Rowspan <= 0);
|
|||
|
if (flag)
|
|||
|
{
|
|||
|
j++;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
stringBuilder.Length = 0;
|
|||
|
stringBuilder.Append(" <td");
|
|||
|
bool flag2 = cell != null && cell.Rowspan > 1;
|
|||
|
if (flag2)
|
|||
|
{
|
|||
|
stringBuilder.Append(" rowspan='" + cell.Rowspan.ToString() + "'");
|
|||
|
}
|
|||
|
bool flag3 = cell != null && cell.Colspan > 1;
|
|||
|
if (flag3)
|
|||
|
{
|
|||
|
stringBuilder.Append(" colspan='" + cell.Colspan.ToString() + "'");
|
|||
|
}
|
|||
|
stringBuilder.AppendFormat(" style='width:{0}px;", (cell == null) ? ((float)columnHeader.Width) : cell.Width);
|
|||
|
bool flag4 = false;
|
|||
|
bool flag5 = cell != null;
|
|||
|
if (flag5)
|
|||
|
{
|
|||
|
bool flag6 = cell.RenderHorAlign == ReoGridRenderHorAlign.Right;
|
|||
|
if (flag6)
|
|||
|
{
|
|||
|
RGHTMLExporter.WriteHtmlStyle(stringBuilder, "text-align", "right");
|
|||
|
flag4 = true;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
bool flag7 = cell.RenderHorAlign == ReoGridRenderHorAlign.Center;
|
|||
|
if (flag7)
|
|||
|
{
|
|||
|
RGHTMLExporter.WriteHtmlStyle(stringBuilder, "text-align", "center");
|
|||
|
flag4 = true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
WorksheetRangeStyle cellStyles = sheet.GetCellStyles(i, j);
|
|||
|
bool flag8 = cellStyles != null;
|
|||
|
if (flag8)
|
|||
|
{
|
|||
|
bool flag9 = cellStyles.HasStyle(PlainStyleFlag.BackColor) && cellStyles.BackColor != Color.White;
|
|||
|
if (flag9)
|
|||
|
{
|
|||
|
RGHTMLExporter.WriteHtmlStyle(stringBuilder, "background-color", TextFormatHelper.EncodeColor(cellStyles.BackColor));
|
|||
|
}
|
|||
|
bool flag10 = cellStyles.HasStyle(PlainStyleFlag.TextColor) && cellStyles.TextColor != Color.Black;
|
|||
|
if (flag10)
|
|||
|
{
|
|||
|
RGHTMLExporter.WriteHtmlStyle(stringBuilder, "color", TextFormatHelper.EncodeColor(cellStyles.TextColor));
|
|||
|
}
|
|||
|
bool flag11 = cellStyles.HasStyle(PlainStyleFlag.FontSize);
|
|||
|
if (flag11)
|
|||
|
{
|
|||
|
RGHTMLExporter.WriteHtmlStyle(stringBuilder, "font-size", cellStyles.FontSize.ToString() + "pt");
|
|||
|
}
|
|||
|
bool flag12 = !flag4 && cellStyles.HasStyle(PlainStyleFlag.HorizontalAlign);
|
|||
|
if (flag12)
|
|||
|
{
|
|||
|
RGHTMLExporter.WriteHtmlStyle(stringBuilder, "text-align", XmlFileFormatHelper.EncodeHorizontalAlign(cellStyles.HAlign));
|
|||
|
}
|
|||
|
bool flag13 = cellStyles.HasStyle(PlainStyleFlag.VerticalAlign);
|
|||
|
if (flag13)
|
|||
|
{
|
|||
|
RGHTMLExporter.WriteHtmlStyle(stringBuilder, "vertical-align", XmlFileFormatHelper.EncodeVerticalAlign(cellStyles.VAlign));
|
|||
|
}
|
|||
|
}
|
|||
|
RangeBorderInfoSet rangeBorders = sheet.GetRangeBorders((cell == null) ? new RangePosition(i, j, 1, 1) : new RangePosition(cell.InternalRow, cell.InternalCol, (int)cell.Rowspan, (int)cell.Colspan), BorderPositions.All, true);
|
|||
|
bool flag14 = !rangeBorders.Top.IsEmpty;
|
|||
|
if (flag14)
|
|||
|
{
|
|||
|
RGHTMLExporter.WriteCellBorder(stringBuilder, "border-top", rangeBorders.Top);
|
|||
|
}
|
|||
|
bool flag15 = !rangeBorders.Left.IsEmpty;
|
|||
|
if (flag15)
|
|||
|
{
|
|||
|
RGHTMLExporter.WriteCellBorder(stringBuilder, "border-left", rangeBorders.Left);
|
|||
|
}
|
|||
|
bool flag16 = !rangeBorders.Right.IsEmpty;
|
|||
|
if (flag16)
|
|||
|
{
|
|||
|
RGHTMLExporter.WriteCellBorder(stringBuilder, "border-right", rangeBorders.Right);
|
|||
|
}
|
|||
|
bool flag17 = !rangeBorders.Bottom.IsEmpty;
|
|||
|
if (flag17)
|
|||
|
{
|
|||
|
RGHTMLExporter.WriteCellBorder(stringBuilder, "border-bottom", rangeBorders.Bottom);
|
|||
|
}
|
|||
|
stringBuilder.Append("'>");
|
|||
|
streamWriter.WriteLine(stringBuilder.ToString());
|
|||
|
bool flag18 = cell != null;
|
|||
|
string value;
|
|||
|
if (flag18)
|
|||
|
{
|
|||
|
value = (string.IsNullOrEmpty(cell.DisplayText) ? " " : RGHTMLExporter.HtmlEncode(cell.DisplayText));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
value = " ";
|
|||
|
}
|
|||
|
streamWriter.WriteLine(value);
|
|||
|
streamWriter.WriteLine(" </td>");
|
|||
|
j += (int)((cell == null) ? 1 : cell.Colspan);
|
|||
|
}
|
|||
|
}
|
|||
|
streamWriter.WriteLine(" </tr>");
|
|||
|
}
|
|||
|
streamWriter.WriteLine(" </table>");
|
|||
|
if (htmlHeader)
|
|||
|
{
|
|||
|
streamWriter.WriteLine("</body>");
|
|||
|
streamWriter.WriteLine("</html>");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static string HtmlEncode(string text)
|
|||
|
{
|
|||
|
bool flag = text == null;
|
|||
|
string result;
|
|||
|
if (flag)
|
|||
|
{
|
|||
|
result = null;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
StringBuilder stringBuilder = new StringBuilder(text.Length + (int)Math.Ceiling((double)((float)text.Length * 0.3f)));
|
|||
|
int length = text.Length;
|
|||
|
int i = 0;
|
|||
|
while (i < length)
|
|||
|
{
|
|||
|
char c = text[i];
|
|||
|
char c2 = c;
|
|||
|
if (c2 <= '&')
|
|||
|
{
|
|||
|
if (c2 != '"')
|
|||
|
{
|
|||
|
if (c2 != '&')
|
|||
|
{
|
|||
|
goto IL_C3;
|
|||
|
}
|
|||
|
stringBuilder.Append("&");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
stringBuilder.Append(""");
|
|||
|
}
|
|||
|
}
|
|||
|
else if (c2 != '\'')
|
|||
|
{
|
|||
|
if (c2 != '<')
|
|||
|
{
|
|||
|
if (c2 != '>')
|
|||
|
{
|
|||
|
goto IL_C3;
|
|||
|
}
|
|||
|
stringBuilder.Append(">");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
stringBuilder.Append("<");
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
stringBuilder.Append("'");
|
|||
|
}
|
|||
|
IL_D4:
|
|||
|
i++;
|
|||
|
continue;
|
|||
|
IL_C3:
|
|||
|
stringBuilder.Append(text[i]);
|
|||
|
goto IL_D4;
|
|||
|
}
|
|||
|
result = stringBuilder.ToString();
|
|||
|
}
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
private static void WriteHtmlStyle(StringBuilder sb, string name, string value)
|
|||
|
{
|
|||
|
sb.AppendFormat("{0}:{1};", name, value);
|
|||
|
}
|
|||
|
|
|||
|
private static void WriteCellBorder(StringBuilder sb, string name, RangeBorderStyle borderStyle)
|
|||
|
{
|
|||
|
RGHTMLExporter.WriteHtmlStyle(sb, name, string.Format("{0} {1}", RGHTMLExporter.ToHTMLBorderLineStyle(borderStyle.Style), TextFormatHelper.EncodeColor(borderStyle.Color)));
|
|||
|
}
|
|||
|
|
|||
|
private static string ToHTMLBorderLineStyle(BorderLineStyle borderLineStyle)
|
|||
|
{
|
|||
|
string result;
|
|||
|
switch (borderLineStyle)
|
|||
|
{
|
|||
|
default:
|
|||
|
result = "solid 1px";
|
|||
|
break;
|
|||
|
case BorderLineStyle.Dotted:
|
|||
|
result = "dotted 1px";
|
|||
|
break;
|
|||
|
case BorderLineStyle.Dashed:
|
|||
|
case BorderLineStyle.Dashed2:
|
|||
|
case BorderLineStyle.DashDot:
|
|||
|
case BorderLineStyle.DashDotDot:
|
|||
|
result = "dashed 1px";
|
|||
|
break;
|
|||
|
case BorderLineStyle.BoldDashDot:
|
|||
|
case BorderLineStyle.BoldDashDotDot:
|
|||
|
case BorderLineStyle.BoldDashed:
|
|||
|
result = "dashed 2px";
|
|||
|
break;
|
|||
|
case BorderLineStyle.BoldDotted:
|
|||
|
result = "dotted 2px";
|
|||
|
break;
|
|||
|
case BorderLineStyle.BoldSolid:
|
|||
|
result = "solid 2px";
|
|||
|
break;
|
|||
|
case BorderLineStyle.BoldSolidStrong:
|
|||
|
result = "solid 3px";
|
|||
|
break;
|
|||
|
}
|
|||
|
return result;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|