标签:
因为有4000个style的限制。所以加了个class,保存style。。。重复的style不新new,只有是新的style才new一个style. 这样基本不会达到4000个的限制。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
namespace TestNPOI
{
public class CellStyle
{
public CellStyle(HSSFWorkbook workbook)
{
Wk = workbook;
_cStyle = Wk.CreateCellStyle();
_font = Wk.CreateFont();
}
public static HSSFWorkbook Wk { get; set; }
public short FillForegroundColor { get; set; }
public short FontColor { get; set; }
// left align by default.
public HorizontalAlignment Alignment { get; set; }
public string Type { get; set; }
//public string IsFontBold { get; set; }
//public string HorizontalAlign { get; set; }
//public string Underscore { get; set; }
private ICellStyle _cStyle;
private readonly IFont _font;
public ICellStyle CStyle
{
get
{
_cStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
_cStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
_cStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
_cStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
_cStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.WHITE.index;
_cStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;
_cStyle.FillForegroundColor = this.FillForegroundColor;
if (Type == "header")
{
_font.Boldweight = (short) FontBoldWeight.BOLD;
_cStyle.WrapText = false;
}
else
{
_font.Boldweight = (short)FontBoldWeight.None;
}
_font.FontName = "verdana"; //
//_cStyle.WrapText = true;
_font.Color = this.FontColor;
_cStyle.SetFont(_font);
_cStyle.Alignment = this.Alignment;
return _cStyle;
}
set { this._cStyle = value; }
}
}
}
下面的是把html string输出到ezcel,这个是针对我的table的,其他的自己慢慢改吧。。。。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Chilkat;
using NPOI.HSSF.Record.CF;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
namespace TestNPOI
{
public class NPOIHelper
{
private readonly List<CellStyle> _userStyleList = new List<CellStyle>();
/// <summary>
/// write html xml string to excel using npoi.
/// </summary>
/// <param name="sHtml"></param>
public void WriteHtmlToExcel(string sHtml)//byte[] WriteHtmlToExcel(string sHtml)
{
// the most important thing is to get a xml string...
// 1. remove tags that are not closed (maybe html, but not xml friendly)
// 2. remove special string...
List<string> multiplePatterns = new List<string>
{
"title=\".*?\"",
"onmouseover=\".*?\"",
"ondblclick=\".*?\"",
"<br>",
"<BR>",
"<img border=0 alt=‘Show Attachment‘ src=‘Images/IconImage.gif‘>",
" ",
"<img src=\"Images.*?>",
"<input .*?>",
//"<td >.*?<input name=\".*?>.*?</td>"
};
sHtml = Regex.Replace(sHtml, string.Join("|", multiplePatterns), string.Empty);
var doc = XDocument.Parse(sHtml);
HSSFWorkbook wk = new HSSFWorkbook();
ISheet sheet = wk.CreateSheet("Report1");
ICellStyle style = wk.CreateCellStyle();
// initialize workbook color pallette.
style.FillForegroundColor = HtmltoColorIndex(wk, "#CCCC99");
string[] styleall = { "background", "align", "color" }; // "text-dec","wrap"
// get header tr
var queryheader = doc.Descendants("tr").Take(1);
// get header th
int count = queryheader.Descendants().Count();
IRow row = sheet.CreateRow(0);
for (int i = 0; i < count; i++)
{
ICell cell = row.CreateCell(i);
XElement xe = queryheader.Descendants().ElementAtOrDefault(i);
foreach (string s in styleall)
{
GetStyleAll(xe, wk, ref style, s);
}
int index = CheckDuplicateUserStyle(style, wk, "header");
cell.SetCellValue(xe.Value.Trim());
cell.CellStyle = _userStyleList[index].CStyle;
sheet.AutoSizeColumn(i);
}
for (int i = 0; i < doc.Descendants("tr").Count() - 1; i++)
{
// get data style
var dataleft = doc.Descendants("tr").Skip(i + 1).Take(1);
IRow newRow = sheet.CreateRow(i + 1);
var datarow = dataleft.Descendants("td");
for (int j = 0; j < datarow.Count(); j++)
{
XElement dataxe = datarow.ElementAtOrDefault(j);
ICell newCell = newRow.CreateCell(j);
foreach (string s in styleall)
{
GetStyleAll(dataxe, wk, ref style, s);
}
int index = CheckDuplicateUserStyle(style, wk, "data");
newCell.SetCellValue(dataxe.Value.Trim());
newCell.CellStyle = _userStyleList[index].CStyle;
sheet.AutoSizeColumn(i);
}
}
// download scripts
using (MemoryStream ms = new MemoryStream())
{
wk.Write(ms);
//// from web.
//HttpContext.Current.Response.ContentType = "application/octet-stream";
//HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=report.xls");
//HttpContext.Current.Response.BinaryWrite(ms.ToArray());
//// from console -> write to file.
string dt = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
using (FileStream fs = new FileStream(@"C:\Users\Richard\Documents\tools\TestNPOI\TestNPOI\bin\" + dt + ".xls", FileMode.Create))
{
ms.WriteTo(fs);
}
//// from web/ console, attached to email.
//return ms.ToArray();
}
}
public void ConvertCellStyleToUserStyle(ICellStyle cellStyle, CellStyle userCellStyle, HSSFWorkbook wk)
{
userCellStyle.FillForegroundColor = cellStyle.FillBackgroundColor;
userCellStyle.FontColor = cellStyle.GetFont(wk).Color;
userCellStyle.Alignment = cellStyle.Alignment;
}
public int CheckDuplicateUserStyle(ICellStyle cellStyle, HSSFWorkbook wk, string type)
{
int index = _userStyleList.FindIndex(t => t.FontColor == cellStyle.GetFont(wk).Color && t.FillForegroundColor == cellStyle.FillForegroundColor && t.Alignment == cellStyle.Alignment);
if (index >= 0) return index;
CellStyle user = new CellStyle(wk)
{
FontColor = cellStyle.GetFont(wk).Color,
FillForegroundColor = cellStyle.FillForegroundColor,
Alignment = cellStyle.Alignment,
Type = type
};
_userStyleList.Add(user);
return _userStyleList.Count - 1;
}
public void GetStyleAll(XElement xe, HSSFWorkbook wk, ref ICellStyle cellStyle, string itemstyle)
{
int pos_start = -1;
int pos_len = -1;
switch (itemstyle)
{
case "background": // background color <-> foreground color in excel
pos_start = 17;
pos_len = 7;
break;
case "text-dec": // underline... actually, no need to do this... can use regex... but no time to change the code....
pos_start = 16;
pos_len = 9;
break;
case "color": // font color
pos_start = 6;
pos_len = 7; // changeable
break;
case "align": // alignment
pos_start = 6;
pos_len = 4; // changeable
break;
//case "wrap":
// break;
default:
pos_start = 0;
pos_len = 0;
break;
}
if (itemstyle == "align")
{
if (xe.Attribute("align") != null)
{
string attr = xe.Attribute("align").Value;
if (attr == "left")
{
cellStyle.Alignment = HorizontalAlignment.LEFT;
}
else if (attr == "right")
{
cellStyle.Alignment = HorizontalAlignment.RIGHT;
}
else
{
cellStyle.Alignment = HorizontalAlignment.CENTER;
}
}
else
cellStyle.Alignment = HorizontalAlignment.LEFT;
}
//else if (itemstyle == "wrap")
//{
// if (xe.Attribute("wrap") != null)
// {
// string attr = xe.Attribute("white-space").Value;
// cellStyle.WrapText = attr == "nowrap;";
// }
// else
// {
// cellStyle.WrapText = false;
// }
//}
else if (itemstyle == "text-dec")
{
if (xe.Attribute("style") != null && xe.Attribute("style").Value.Contains(itemstyle))
{
GetBackColor(xe.Attribute("style").Value, wk,ref cellStyle, pos_start, pos_len, itemstyle);
}
}
else if (itemstyle == "color" || itemstyle == "background")
{
if (xe.Attribute("style") != null && xe.Attribute("style").Value.Contains(itemstyle))
{
GetBackColor(xe.Attribute("style").Value, wk,ref cellStyle, pos_start, pos_len, itemstyle);
}
else
{
if (xe.Parent.Attribute("style") != null && xe.Parent.Attribute("style").Value.Contains(itemstyle))
{
GetBackColor(xe.Parent.Attribute("style").Value, wk,ref cellStyle, pos_start, pos_len, itemstyle);
}
else
{
if (itemstyle == "background")
cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.WHITE.index;
}
}
}
}
/// <summary>
/// need to re-write if have time....
/// </summary>
/// <param name="attr"></param>
/// <param name="wk"></param>
/// <param name="cellStyle"></param>
/// <param name="posStart"></param>
/// <param name="posLen"></param>
/// <param name="type"></param>
public void GetBackColor(string attr, HSSFWorkbook wk, ref ICellStyle cellStyle, int posStart, int posLen, string type)
{
int posDes = 0;
short npoicolor = 0;
posDes = attr.IndexOf(type, StringComparison.Ordinal);
switch (type)
{
case "background":
//Regex regbackcol = new Regex("background-color:[\\w]+;");
string myBackString = string.Empty;
Regex regbackcol = new Regex("background-color:*([^;]+)");
Match matchbackcol = regbackcol.Match(attr);
if (matchbackcol.Success)
{
string colstr = matchbackcol.Value;
if (colstr.Contains("#"))
{
myBackString = colstr.Substring(posStart, posLen);
}
else if (colstr.Contains("rgb"))
{
string col = colstr.Substring(posStart, colstr.Length - posStart).Trim();
string colrgb = col.Substring(4, col.Length - 4);
int colr = int.Parse(colrgb.Split(‘,‘)[0]);
int colg = int.Parse(colrgb.Split(‘,‘)[1]);
int colb = int.Parse(colrgb.Split(‘,‘)[2]);
myBackString = String.Format("#{0:X2}{1:X2}{2:X2}", colr, colg, colb);
}
else
{
string col = colstr.Substring(posStart, colstr.Length - posStart);
Color color = Color.FromName(col);
myBackString = String.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
}
npoicolor = HtmltoColorIndex(wk, myBackString);
if (npoicolor != 0)
{
cellStyle.FillForegroundColor = npoicolor;
}
}
else
{
cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.WHITE.index;
}
break;
case "align":
break;
case "text-dec":
IFont fontdec = wk.GetFontAt(0);
fontdec.Underline = FontFormatting.U_SINGLE;
cellStyle.SetFont(fontdec);
break;
case "color":
attr = attr.Replace("-color", "");
int posCol = -1;
posCol = attr.IndexOf(type);
if (posCol >= 0)
{
if (attr.Contains("#"))
{
string col = attr.Substring(posDes + posStart, posLen);
short index = HtmltoColorIndex(wk, col);
IFont font = wk.GetFontAt(0); //////////////////
font.Color = index;
font.FontName = "verdana";
cellStyle.SetFont(font);
}
else
{
Regex reg = new Regex("color:[\\w]+");
Match match = reg.Match(attr);
if (match.Success)
{
string colstr = match.Value;
string col = colstr.Substring(posStart, colstr.Length - posStart);
Color color = Color.FromName(col);
string myHexString = String.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
short index = HtmltoColorIndex(wk, myHexString);
IFont font = wk.GetFontAt(0);///////////////
font.Color = index;
font.FontName = "verdana";
cellStyle.SetFont(font);
}
}
}
break;
}
}
public short HtmltoColorIndex(HSSFWorkbook book, string htmlcolor)
{
short t = 0;
System.Drawing.Color color = System.Drawing.ColorTranslator.FromHtml(htmlcolor);
HSSFPalette palette = book.GetCustomPalette();
HSSFColor hssfColor = palette.FindColor(color.R, color.G, color.B);
if (hssfColor == null)
{
if (NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE < 255)
{
if (NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE < 64)
{
NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE = 64;
NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE += 1;
hssfColor = palette.AddColor(color.R, color.G, color.B);
hssfColor = palette.FindColor(color.R, color.G, color.B);
}
else
{
hssfColor = palette.FindSimilarColor(color.R, color.G, color.B);
}
t = hssfColor.GetIndex();
}
}
else
{
t = hssfColor.GetIndex();
}
return t;
}
public void AttachExcelToEmail(byte[] buff, Email email, string fileName)
{
email.AddDataAttachment(fileName, buff);
}
}
}
标签:
原文地址:http://www.cnblogs.com/windy1024/p/5351779.html