epplus输出成thml
2024-08-31 19:36:37
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OfficeOpenXml;
using System.IO;
using EPPlus.Html;
namespace testepplus
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string zpath = Server.MapPath("/") + "test1.xlsx";
FileInfo zfile = new FileInfo(zpath); using (ExcelPackage excel = new ExcelPackage(zfile,"123"))
{
//excel.SaveAs(new FileInfo(Server.MapPath("/") + "test1.xlsx"), "123");
Response.Write(EPPlus.Html.EPPlusExtensions.ToHtml(excel.Workbook.Worksheets[3])); } }
}
}
fx要4.5+ ,需要huget package epplus ,epplus.html
可以从https://github.com/fitogram/EPPlus.Html 下载源码更改fx为4.0
epplus.html 0.20版本不支持excel里的图片
修改epplus.html添加支持图片的不成熟的源码
using EPPlus.Html.Converters;
using EPPlus.Html.Html;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Drawing;
namespace EPPlus.Html
{
public static class EPPlusExtensions
{
public static string ToHtml(this ExcelWorksheet sheet)
{
int lastRow = sheet.Dimension.Rows;
int lastCol = sheet.Dimension.Columns; HtmlElement htmlTable = new HtmlElement("table");
htmlTable.Attributes["cellspacing"] = 0;
htmlTable.Styles["white-space"] = "nowrap"; //render rows
for (int row = 1; row <= lastRow; row++)
{
ExcelRow excelRow = sheet.Row(row); var test = excelRow.Style;
HtmlElement htmlRow = htmlTable.AddChild("tr");
htmlRow.Styles.Update(excelRow.ToCss()); for (int col = 1; col <= lastCol; col++)
{
ExcelRange excelCell = sheet.Cells[row, col];
HtmlElement htmlCell = htmlRow.AddChild("td"); string cc = getPictureCode(row, col, sheet);
if (cc == "")
{
htmlCell.Content = excelCell.Text;
} else
{
htmlCell.Content = cc;
}
htmlCell.Styles.Update(excelCell.ToCss());
}
} return htmlTable.ToString();
} public static string getPictureCode(int row ,int col , ExcelWorksheet sheet)
{
foreach (OfficeOpenXml.Drawing.ExcelDrawing excelDrawing in sheet.Drawings)
{
OfficeOpenXml.Drawing.ExcelPicture excelPicture = excelDrawing as OfficeOpenXml.Drawing.ExcelPicture;
if (excelPicture.From.Column+1==col && excelPicture.From.Row+1 == row){
return "<img src = \"data:image/jpeg;base64," + ImgToBase64String(excelPicture.Image) + "\"/>";
} }
return "";
} public static CssInlineStyles ToCSS(this ExcelStyles styles)
{
throw new NotImplementedException();
} public static string ImgToBase64String(Image image)
{
MemoryStream ms = new MemoryStream();
try
{ image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
return Convert.ToBase64String(arr);
}
catch (Exception ex)
{
return "";
}
finally
{
ms.Close();
}
} } }
最新文章
- 【Oracle】表空间容量修改
- {A} + {B} 分类: HDU 2015-07-11 18:06 6人阅读 评论(0) 收藏
- php大力力 [016节] 兄弟连高洛峰php教程(2014年 14章数据库章节列表)
- Android学习笔记之AndroidManifest.xml文件解析
- [COCOS2DX]官网helloworld在VS2012中的部署
- MAC地址查询 Linux/Unix操作系统mac地址怎么查
- 设计模式入门之桥接模式Bridge
- CSU 1515 Sequence
- HBuilder的webview操作
- selenium+python开发环境的搭建
- FWT模板
- 浅析布隆过滤器及实现demo
- 1344:【例4-4】最小花费 dijkstra
- python冒泡排序法
- C语言基础三(敲打键盘、寻找资料,循环语句)
- jq ‘’操作‘’伪元素
- Halcon常用算子01
- Django 时间与时区设置问题
- soj1010. Zipper
- 6 TensorFlow实现cnn识别手写数字
热门文章
- 【Windows Defender Antivirus Service 永久禁用 】
- uva 1519 - Dictionary Size(字典树)
- gen_server的enter_loop分析
- BZOJ1415 聪聪与可可 - 期望dp
- PCI的imagework已由freeview软件代替
- 通过一次SpringBoot打成war包部署到tomcat启动总结一般jar包冲突的解决方法
- rac下一个/tmp/bootstrap权限问题
- Sleep(0)的妙用
- java写文件时往末尾追加文件(而不是覆盖原文件),的两种方法总结
- POJ 3904(容斥原理)