SongjianBiaoMain model下面 mxlist 是明细信息
并且在指定的单元格下插入图片
#region 数据导入模板操作
/// <summary>
/// 导出到对应的模板下
/// </summary>
/// <param name="Model">主数据</param>
/// <param name="LastUsedMB">模板设定数据</param>
/// <param name="DOCTempPath">模板文件地址</param>
/// <param name="DocSavePath">保存地址</param>
/// <param name="Hangshu">明细表下条数(大于这个数据需要插入行)</param>
/// <param name="IndexNoStrat">从第几个开始插入</param>
/// <param name="OngPageRows">一页行数</param>
/// <returns></returns>
public static ResultMsg ExcelOutToTemp(SongjianBiaoMain Model, MobanSet LastUsedMB, string DOCTempPath, string DocSavePath, int Hangshu, int OnePageRows)
{
ResultMsg msg = new ResultMsg();
IWorkbook workbook = null;
ISheet Modelsheet = null;
FileStream fs = new FileStream(DOCTempPath, FileMode.Open, FileAccess.Read);
workbook = new XSSFWorkbook(fs);
Modelsheet = workbook.GetSheetAt(0);//获取第一个工作表
IWorkbook myXSSFworkbook = new XSSFWorkbook();
int PageCount = (int)Math.Ceiling((decimal)Model.Mxlist.Count / (decimal)OnePageRows);
for (int i = 0; i < PageCount; i++)
{
//ISheet sheet = workbook.CreateSheet(i.ToString()) as XSSFSheet;
Modelsheet.CopyTo(workbook, $"检验检测报告单{i}", true, true);//创建工作表
ISheet sheet = workbook.GetSheet($"检验检测报告单{i}");//获取刚创建的工作表
if (sheet != null)
{
int LocalRow = 3;//获取写入的第一行
IRow SetThisRow = sheet.GetRow(LocalRow);
SetThisRow.Cells[4].SetCellValue(Model.CaiyangDanwei);///采样单位
LocalRow++;
IRow SongyangThisRow = sheet.GetRow(LocalRow);
SongyangThisRow.Cells[1].SetCellValue(Model.CaiyangDanwei);///送样单位
SongyangThisRow.Cells[4].SetCellValue(Model.CaiyangRiqi);///采样日期
LocalRow++;
IRow YanshouThisRow = sheet.GetRow(LocalRow);
YanshouThisRow.Cells[1].SetCellValue(Model.Mxlist.Count);///样品数量
YanshouThisRow.Cells[4].SetCellValue(Model.SongjianShijian);///收验日期
LocalRow++;
IRow QizhiThisRow = sheet.GetRow(LocalRow);
QizhiThisRow.Cells[1].SetCellValue(Model.QizhiRiqi); ///检验起止日 //检验项目//检验方法
LocalRow += 2;
IRow YijuThisRow = sheet.GetRow(LocalRow);//依据行
YijuThisRow.Cells[1].SetCellValue(LastUsedMB.JianceYiju);///依据
LocalRow++;//检验结果
//IRow JieguoThisRow = sheet.GetRow(LocalRow);//结果行
//JieguoThisRow.Cells[1].SetCellValue(LastUsedMB.JianceYiju);///结果
//LocalRow += 23;
//ThisRowCaozuoZhe.Cells[1].SetCellValue(LastUsedMB.JianyanZhe);
//ThisRowCaozuoZhe.Cells[3].SetCellValue(LastUsedMB.ShenheZhe);
//LocalRow += 2;
IRow ThisRowRiqi = sheet.GetRow(17);
ThisRowRiqi.Cells[4].SetCellValue(Model.BaogaoRiqi);//报告日期
LocalRow += 3;//跳至数据行
int Suoxuhangshu = Model.Mxlist.Count - OnePageRows * i > OnePageRows ? OnePageRows : Model.Mxlist.Count - OnePageRows * i;//所需行数
if (Suoxuhangshu > Hangshu)//插入新行
{
int StartRowIndex = LocalRow + Hangshu;
int sourceRowIndex = LocalRow + Hangshu - 1;
CopyRow(sheet, StartRowIndex, sourceRowIndex, Suoxuhangshu - Hangshu);
}
for (int j = 0; j < OnePageRows; j++)
{
int ThisIndex = j + OnePageRows * i;
if (ThisIndex >= Model.Mxlist.Count)
{
break;
}
IRow ThisRowInsertData = sheet.GetRow(LocalRow);
ThisRowInsertData.Cells[0].SetCellValue(ThisIndex + 1);
ThisRowInsertData.Cells[1].SetCellValue(Model.Mxlist[ThisIndex].ReyuanLeibie);
ThisRowInsertData.Cells[2].SetCellValue(Model.Mxlist[ThisIndex].Xingming);
ThisRowInsertData.Cells[3].SetCellValue(Model.Mxlist[ThisIndex].CaiyangCishu);
ThisRowInsertData.Cells[4].SetCellValue(Model.Mxlist[ThisIndex].ORF1ab);
ThisRowInsertData.Cells[5].SetCellValue(Model.Mxlist[ThisIndex].NJ);
LocalRow++;
}
LocalRow = LocalRow + 2;//签字数据
//IRow ThisRowCaozuoZhe = sheet.GetRow(LocalRow+3);
string JianyanZheFile = GModel.ImageFile + LastUsedMB.JianyanZheFile;
string ShenheZheFile = GModel.ImageFile + LastUsedMB.ShenheZheFile;
if (File.Exists(JianyanZheFile))
{
Image JianyanZheImage = Common.ImageHelper.FileToImage(JianyanZheFile);
InsertImage(sheet, workbook, JianyanZheImage, LocalRow, 1);
}
if (File.Exists(ShenheZheFile))
{
Image ShenheZheImage = Common.ImageHelper.FileToImage(ShenheZheFile);
InsertImage(sheet, workbook, ShenheZheImage, LocalRow, 3);
}
}
else
{
msg.Success = false;
msg.ErrMsg = "未找到表格";
}
}
workbook.RemoveSheetAt(0);//去掉模板文件
//保存文件
FileStream streamFile = new FileStream(DocSavePath, FileMode.Create);
System.IO.MemoryStream streamMemory = new System.IO.MemoryStream();
workbook.Write(streamMemory);
byte[] data = streamMemory.ToArray();
// 开始写入
streamFile.Write(data, 0, data.Length);
// 清空缓冲区、关闭流
streamFile.Flush();
streamFile.Close();
myXSSFworkbook = null;
streamMemory.Close();
streamMemory.Dispose();
msg.Success = true;
return msg;
}
#endregion
/// <summary>
/// 复制行格式并插入指定行数
/// </summary>
/// <param name="sheet">当前sheet</param>
/// <param name="startRowIndex">起始行位置</param>
/// <param name="sourceRowIndex">模板行位置</param>
/// <param name="insertCount">插入行数</param>
public static void CopyRow(ISheet sheet, int startRowIndex, int sourceRowIndex, int insertCount)
{
IRow sourceRow = sheet.GetRow(sourceRowIndex);
int sourceCellCount = sourceRow.Cells.Count;
//1. 批量移动行,清空插入区域
sheet.ShiftRows(startRowIndex, //开始行
sheet.LastRowNum, //结束行
insertCount, //插入行总数
true, //是否复制行高
false //是否重置行高
);
int startMergeCell = -1; //记录每行的合并单元格起始位置
for (int i = startRowIndex; i < startRowIndex + insertCount; i++)
{
IRow targetRow = null;
ICell sourceCell = null;
ICell targetCell = null;
targetRow = sheet.CreateRow(i);
targetRow.Height = sourceRow.Height;//复制行高
for (int m = sourceRow.FirstCellNum; m < sourceRow.LastCellNum; m++)
{
sourceCell = sourceRow.GetCell(m);
if (sourceCell == null)
continue;
targetCell = targetRow.CreateCell(m);
targetCell.CellStyle = sourceCell.CellStyle;//赋值单元格格式
targetCell.SetCellType(sourceCell.CellType);
if ((sourceCell.IsMergedCell && !string.IsNullOrWhiteSpace(sourceCell.StringCellValue)))
{
if (startMergeCell < m - 1)//如果第一个是合并单元格并且是竖行的合并单元格就就取消合并
{
if (startMergeCell >= 0)
{
sheet.AddMergedRegion(new CellRangeAddress(i, i, startMergeCell, m - 1));
}
}
startMergeCell = m;
}
else if ((sourceCell.IsMergedCell && string.IsNullOrWhiteSpace(sourceCell.StringCellValue)) || m == sourceRow.LastCellNum - 1)
{
if (m == sourceRow.LastCellNum - 1 && startMergeCell >= 0)
{
sheet.AddMergedRegion(new CellRangeAddress(i, i, startMergeCell, m));
}
continue;
}
else
{
if (startMergeCell < m - 1)//如果第一个是合并单元格并且是竖行的合并单元格就就取消合并
{
if (startMergeCell >= 0)
{
sheet.AddMergedRegion(new CellRangeAddress(i, i, startMergeCell, m - 1));
startMergeCell = -1;
}
}
else
{
startMergeCell = -1;
}
}
}
}
}
public static void InsertImage(ISheet sheet, IWorkbook workBook, Image Img, int RowIndex, int ColumIndex)
{
byte[] buffer = Common.ImageHelper.ImageToBytesByFormart(Img);
int pictureIdx = workBook.AddPicture(buffer, NPOI.SS.UserModel.PictureType.PNG);//Byte图片文件Byte[]
IDrawing DrawsCell = sheet.CreateDrawingPatriarch();
IClientAnchor anchor = DrawsCell.CreateAnchor(0, 0, 1023, 0, ColumIndex, RowIndex, ColumIndex + 1, RowIndex + 1);//插入数据的位置(关键是最后四个参数:开始列,开始行,结束列,结束行)
IPicture Pict = DrawsCell.CreatePicture(anchor, pictureIdx);
//pict.Resize(); //数据刷线
}