NOPI Excel插件导入导出 图片批注
dateTable导出到excel的MemoryStream
/// <summary>
/// DataTable导出到Excel的MemoryStream Export()
/// </summary>
/// <param name="dtSource">DataTable数据源</param>
/// <param name="excelConfig">导出设置包含文件名、标题、列设置</param>
/// <param name="isRemoveColumns"></param>
public static MemoryStream ExportMemoryStream(DataTable dtSource, ExcelConfig excelConfig, bool isRemoveColumns = false)
{
if (isRemoveColumns)
{
int colint = ;
for (int i = ; i < dtSource.Columns.Count; )
{
DataColumn column = dtSource.Columns[i];
if (colint>=excelConfig.ColumnEntity.Count || excelConfig.ColumnEntity[colint].Column != column.ColumnName)
{
dtSource.Columns.Remove(column.ColumnName);
}
else
{
ColumnEntity columnentity = excelConfig.ColumnEntity.Find(t => t.Column == dtSource.Columns[i].ColumnName);
dtSource.Columns[i].ColumnName = columnentity.ExcelColumn;//修改列头名
i++;
colint++;
} }
} HSSFWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet(); #region 右击文件 属性信息
{
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "NPOI";
workbook.DocumentSummaryInformation = dsi; SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Author = "zdd"; //填加xls文件作者信息
si.ApplicationName = "XX系统"; //填加xls文件创建程序信息
si.LastAuthor = "zdd"; //填加xls文件最后保存者信息
si.Comments = "zdd"; //填加xls文件作者信息
si.Title = "标题信息"; //填加xls文件标题信息
si.Subject = "主题信息";//填加文件主题信息
si.CreateDateTime = System.DateTime.Now;
workbook.SummaryInformation = si;
}
#endregion #region 设置标题样式
ICellStyle headStyle = workbook.CreateCellStyle();
int[] arrColWidth = new int[dtSource.Columns.Count];
string[] arrColName = new string[dtSource.Columns.Count];//列名
ICellStyle[] arryColumStyle = new ICellStyle[dtSource.Columns.Count];//样式表
headStyle.Alignment = HorizontalAlignment.Center; // ------------------
if (excelConfig.Background != new Color())
{
if (excelConfig.Background != new Color())
{
headStyle.FillPattern = FillPattern.SolidForeground;
headStyle.FillForegroundColor = GetXLColour(workbook, excelConfig.Background);
}
}
IFont font = workbook.CreateFont();
font.FontHeightInPoints = excelConfig.TitlePoint;
if (excelConfig.ForeColor != new Color())
{
font.Color = GetXLColour(workbook, excelConfig.ForeColor);
}
font.Boldweight = ;
headStyle.SetFont(font);
#endregion #region 列头及样式
ICellStyle cHeadStyle = workbook.CreateCellStyle();
cHeadStyle.Alignment = HorizontalAlignment.Center; // ------------------
IFont cfont = workbook.CreateFont();
cfont.FontHeightInPoints = excelConfig.HeadPoint;
cHeadStyle.SetFont(cfont);
#endregion #region 设置内容单元格样式
foreach (DataColumn item in dtSource.Columns)
{
ICellStyle columnStyle = workbook.CreateCellStyle();
columnStyle.Alignment = HorizontalAlignment.Center;
arrColWidth[item.Ordinal] = Encoding.GetEncoding().GetBytes(item.ColumnName.ToString()).Length;
arrColName[item.Ordinal] = item.ColumnName.ToString();
if (excelConfig.ColumnEntity != null)
{
ColumnEntity columnentity = excelConfig.ColumnEntity.Find(t => t.Column == item.ColumnName);
if (columnentity != null)
{
arrColName[item.Ordinal] = columnentity.ExcelColumn;
if (columnentity.Width != )
{
arrColWidth[item.Ordinal] = columnentity.Width;
}
if (columnentity.Background != new Color())
{
if (columnentity.Background != new Color())
{
columnStyle.FillPattern = FillPattern.SolidForeground;
columnStyle.FillForegroundColor = GetXLColour(workbook, columnentity.Background);
}
}
if (columnentity.Font != null || columnentity.Point != || columnentity.ForeColor != new Color())
{
IFont columnFont = workbook.CreateFont();
columnFont.FontHeightInPoints = ;
if (columnentity.Font != null)
{
columnFont.FontName = columnentity.Font;
}
if (columnentity.Point != )
{
columnFont.FontHeightInPoints = columnentity.Point;
}
if (columnentity.ForeColor != new Color())
{
columnFont.Color = GetXLColour(workbook, columnentity.ForeColor);
}
columnStyle.SetFont(font);
}
columnStyle.Alignment = getAlignment(columnentity.Alignment);
}
}
arryColumStyle[item.Ordinal] = columnStyle;
}
if (excelConfig.IsAllSizeColumn)
{
#region 根据列中最长列的长度取得列宽
for (int i = ; i < dtSource.Rows.Count; i++)
{
for (int j = ; j < dtSource.Columns.Count; j++)
{
if (arrColWidth[j] != )
{
int intTemp = Encoding.GetEncoding().GetBytes(dtSource.Rows[i][j].ToString()).Length;
if (intTemp > arrColWidth[j])
{
arrColWidth[j] = intTemp;
}
} }
}
#endregion
}
#endregion #region 填充数据 #endregion ICellStyle dateStyle = workbook.CreateCellStyle();
IDataFormat format = workbook.CreateDataFormat();
dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");
int rowIndex = ;
foreach (DataRow row in dtSource.Rows)
{
#region 新建表,填充表头,填充列头,样式
if (rowIndex == || rowIndex == )
{
if (rowIndex != )
{
sheet = workbook.CreateSheet();
} #region 表头及样式
{
if (excelConfig.Title != null)
{
IRow headerRow = sheet.CreateRow();
if (excelConfig.TitleHeight != )
{
headerRow.Height = (short)(excelConfig.TitleHeight * );
}
headerRow.HeightInPoints = ;
headerRow.CreateCell().SetCellValue(excelConfig.Title);
headerRow.GetCell().CellStyle = headStyle;
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(, , , dtSource.Columns.Count - )); // ------------------
} }
#endregion #region 列头及样式
{
IRow headerRow = sheet.CreateRow();
#region 如果设置了列标题就按列标题定义列头,没定义直接按字段名输出
foreach (DataColumn column in dtSource.Columns)
{
headerRow.CreateCell(column.Ordinal).SetCellValue(arrColName[column.Ordinal]);
headerRow.GetCell(column.Ordinal).CellStyle = cHeadStyle;
//设置列宽
sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + ) * );
}
#endregion
}
#endregion rowIndex = ;
}
#endregion #region 填充内容
IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in dtSource.Columns)
{
ICell newCell = dataRow.CreateCell(column.Ordinal);
newCell.CellStyle = arryColumStyle[column.Ordinal];
string drValue = row[column].ToString(); //如果是图片列
if (column.ToString() == "图片")
{ string tPath = DirFileHelper.MapPath("/ExportFile/ImageFiles/");//服务器图片存储目录
if (!DirFileHelper.IsExistDirectory(tPath))//判断是否存在此目录 无则创建
{
DirFileHelper.CreateDir("/ExportFile/ImageFiles/");//创建临时存储压缩后图片路径
}
string filePath = SharedImagePath + drValue;
string newImagePath = tPath + drValue;//压缩后图片绝对路径
if (DirFileHelper.IsExistFile(newImagePath))//若果本地已存在 则不从100共享盘中取图片
{
byte[] bytes = System.IO.File.ReadAllBytes(newImagePath);
int pictureIndex = workbook.AddPicture(bytes, PictureType.JPEG);
HSSFPatriarch par = sheet.CreateDrawingPatriarch() as HSSFPatriarch; HSSFClientAnchor anchor = new HSSFClientAnchor
{
Dx1 = ,//起始单元格的x偏移量,如例子中的255表示直线起始位置距A1单元格左侧的距离;
Dy1 = ,//起始单元格的y偏移量,如例子中的125表示直线起始位置距A1单元格上侧的距离;
Dx2 = ,//终止单元格的x偏移量,如例子中的1023表示直线起始位置距C3单元格左侧的距离;
Dy2 = ,//:终止单元格的y偏移量,如例子中的150表示直线起始位置距C3单元格上侧的距离;
Col1 = column.Ordinal +, //批注起始位置的纵坐标(当前单元格位置+2)
Col2 = column.Ordinal + , //批注结束位置的纵坐标
Row1 = column.Ordinal + , //批注起始位置的横坐标
Row2 = column.Ordinal + //批注结束位置的横坐标
};
HSSFComment comment = par.CreateComment(anchor);
comment.SetBackgroundImage(pictureIndex);
newCell.CellComment = comment;
}
else//取100共享盘图片并压缩指定大小
{
if (DirFileHelper.IsExistFile(filePath))//需要增加判断是否存在此图片
{
ImageHelper.CreateMinImageAndDel(filePath, , , tPath);//压缩指定大小图片
byte[] bytes = System.IO.File.ReadAllBytes(newImagePath);
int pictureIndex = workbook.AddPicture(bytes, PictureType.JPEG);
HSSFPatriarch par = sheet.CreateDrawingPatriarch() as HSSFPatriarch;
HSSFClientAnchor anchor = new HSSFClientAnchor
{
Dx1 = ,//起始单元格的x偏移量,如例子中的255表示直线起始位置距A1单元格左侧的距离;
Dy1 = ,//起始单元格的y偏移量,如例子中的125表示直线起始位置距A1单元格上侧的距离;
Dx2 = ,//终止单元格的x偏移量,如例子中的1023表示直线起始位置距C3单元格左侧的距离;
Dy2 = ,//:终止单元格的y偏移量,如例子中的150表示直线起始位置距C3单元格上侧的距离;
Col1 = column.Ordinal + , //批注起始位置的纵坐标(当前单元格位置+2)
Col2 = column.Ordinal + , //批注结束位置的纵坐标
Row1 = column.Ordinal + , //批注起始位置的横坐标
Row2 = column.Ordinal + //批注结束位置的横坐标
};
HSSFComment comment = par.CreateComment(anchor);
comment.SetBackgroundImage(pictureIndex);
}
}
//newCell.SetCellValue(drValue);
SetCell(newCell, dateStyle, column.DataType, drValue);
}
else
{
SetCell(newCell, dateStyle, column.DataType, drValue);
} }
#endregion
rowIndex++;
}
using (MemoryStream ms = new MemoryStream())
{
workbook.Write(ms);
ms.Flush();
ms.Position = ;
return ms;
}
}
#endregion
NOPI Excel插件导入导出 图片批注的更多相关文章
- OpenXml Excel数据导入导出(含图片的导入导出)
声明:里面的很多东西是基于前人的基础上实现的,具体是哪些人 俺忘了,我做了一些整合和加工 这个项目居于openxml做Excel的导入导出,可以用OpenXml读取Excel中的图片 和OpenXml ...
- poi实现excel的导入导出功能
Java使用poi实现excel的导入导出功能: 工具类ExcelUtil,用于解析和初始化excel的数据:代码如下 package com.raycloud.kmmp.item.service.u ...
- NPOI 在指定单元格导入导出图片
NPOI 在指定单元格导入导出图片 Intro 我维护了一个 NPOI 的扩展,主要用来导入导出 Excel 数据,最近有网友提出了导入 Excel 的时候解析图片的需求,于是就有了本文的探索 导入E ...
- excel的导入导出的实现
1.创建Book类,并编写set方法和get方法 package com.bean; public class Book { private int id; private String name; ...
- c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出
c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出 using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using S ...
- java实现excel的导入导出(poi详解)[转]
java实现excel的导入导出(poi详解) 博客分类: java技术 excel导出poijava 经过两天的研究,现在对excel导出有点心得了.我们使用的excel导出的jar包是poi这个 ...
- NodeJs之EXCEL文件导入导出MongoDB数据库数据
NodeJs之EXCEL文件导入导出MongoDB数据库数据 一,介绍与需求 1.1,介绍 (1),node-xlsx : 基于Node.js解析excel文件数据及生成excel文件. (2),ex ...
- java 中Excel的导入导出
部分转发原作者https://www.cnblogs.com/qdhxhz/p/8137282.html雨点的名字 的内容 java代码中的导入导出 首先在d盘创建一个xlsx文件,然后再进行一系列 ...
- PowerDesigner数据库设计PDM基于Excel的导入导出总结
经常用到pdm来管理代码,一两张表,手写一下还凑合,一旦表多了,就慌了.于是,开始学习用vbs进行Excel的来快速导入导出操作PDM就变得很紧急了,搜罗了网络上的很多vbs脚本,各有各的优点,但对于 ...
随机推荐
- Thymeleaf+Spring整合
前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...
- 对jQuery ajax三级级联的简单研究
最近写程序的时候经常遇到使用ajax获取数据的问题,刚好昨天遇到ajax写三级级联问题,自己写了一个简单的级联.对于服务端获取数据的就不多写了,客户端的ajax发送请求我在这里详细说一下,因为我也没专 ...
- Linux学习 :移植linux-4.7.4到JZ2440开发板
一.编译环境搭建: 1.linux源码下载:https://www.kernel.org/2.安装交叉编译工具链: ①手动下载配置工具链: (1):解压 arm-linux-gcc-3.4.1.tar ...
- JDK动态代理与CGLib动态代理
1.JDK动态代理 JDK1.3以后java提供了动态代理技术,允许开发者在运行期创建接口的代理实例,动态代理是实现AOP的绝好底层技术. JDK的动态代理主要涉及到java.lang.reflect ...
- VM virtuaBox异常关机启动不了的解决方案
事件回放 我的物理机是win7,上面装了一个VM virtualBox,用来装Centos,有天物理机非正常关闭,导致VM virtuaBox异常关机启动不了,如下: 确实找不到这个vm_liang. ...
- JavaScript Cookies
JavaScript Cookies 当 web 服务器向浏览器发送 web 页面时,在连接关闭后,服务端不会记录用户的信息.Cookies 的作用就是用于存储 web 页面的用户信息. Cookie ...
- scala学习----柯里化
1.鸭子类型,走起来像鸭子,叫起来像鸭子,就是鸭子.函数中使用{ def close(): Unit }作为参数类型,因此任何含有此函数的类都可以作为参数传递.好处是不必使用继承特性. def wit ...
- MySQL主从复制与读写分离
MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践 Mysql作为目前世界上使用最广泛的免费数据库,相信所有从事系统运维的工程师都一定接触过.但在实际的生产环境中, ...
- 第十章 嵌入式Linux的调试技术
对调试工具进行简介.Linux中提供了一类工具,通过这些工具可以逐行跟踪程序的代码,用于测试用户空间程序的gdb.gdbserver和调试内核空间程序的kgdb. 用gdb调试用户空间程序:gdb可跟 ...
- vim笔记2
用vim 快两年了 看过教程也不少,总的来说还是得自己多练习,当自己觉得有需要的时候,再添加功能.这里分享个看过的最好的教程,出自贴吧的某个朋友,写的很好 零 学会盲打 壹 配置文件先从最简开始,在 ...