修改運維匯出excel insert多張圖片
This commit is contained in:
parent
93b42a97de
commit
7994d71ed8
@ -6,19 +6,22 @@ using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using NPOI.HPSF;
|
||||
using NPOI.HSSF.UserModel;
|
||||
using NPOI.SS.Formula.Functions;
|
||||
using NPOI.SS.UserModel;
|
||||
using NPOI.Util;
|
||||
using NPOI.XSSF.UserModel;
|
||||
using NPOI.Util;
|
||||
using Org.BouncyCastle.Crypto.Agreement.JPake;
|
||||
using Repository.BackendRepository.Interface;
|
||||
using Repository.FrontendRepository.Interface;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing.Drawing2D;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Image = System.Drawing.Image;
|
||||
|
||||
namespace FrontendWebApi.ApiControllers
|
||||
{
|
||||
@ -226,6 +229,7 @@ namespace FrontendWebApi.ApiControllers
|
||||
/// <returns></returns>
|
||||
public FileResult OpeExportExcel([FromQuery] byte? work_type)
|
||||
{
|
||||
var j = 1;//row
|
||||
var workbook = new XSSFWorkbook();
|
||||
#region excel設定
|
||||
IFont font12 = workbook.CreateFont();
|
||||
@ -366,9 +370,11 @@ namespace FrontendWebApi.ApiControllers
|
||||
{
|
||||
var sqlString = $@"select opr.*, concat(d.device_floor_tag, ' ', d.device_last_name, ' ', d.device_serial_tag) as device_name, ui.full_name as user_full_name
|
||||
from operation_record opr
|
||||
left join device d on opr.fix_do_code = d.device_number
|
||||
left join device d on opr.fix_do_code = d.device_number and d.deleted = 0 and d.device_area_tag = SUBSTRING_INDEX(opr.fix_do_code, '_', 1)
|
||||
and d.device_building_tag = SUBSTRING_INDEX(opr.fix_do_code, '_', 2) and d.device_floor_tag = SUBSTRING_INDEX(opr.fix_do_code, '_', 5)
|
||||
and d.device_system_tag = opr.device_system_category_layer2 and d.device_name_tag = opr.device_system_category_layer3
|
||||
left join userinfo ui on opr.work_person_id = ui.userinfo_guid
|
||||
where opr.deleted = 0 and opr.work_type = 1";
|
||||
where opr.deleted = 0 and opr.work_type = 1;";
|
||||
var sheet = workbook.CreateSheet("廠商資料");
|
||||
var operation_record = backendRepository.GetAllAsync<Operation_Record>(sqlString, null);
|
||||
List<Operation_Record_File> operation_record_file = new List<Operation_Record_File>();
|
||||
@ -464,7 +470,53 @@ namespace FrontendWebApi.ApiControllers
|
||||
}
|
||||
if (i == 7)
|
||||
{
|
||||
cell.SetCellValue(or.finish_time); // image
|
||||
var imageNames = or.lorf.Count > 0 ? or.lorf.Select(x => x.save_file_name).ToList() : null;
|
||||
if (imageNames != null)
|
||||
{
|
||||
//insert image process
|
||||
var dy1 = 10;
|
||||
var dx1 = 10;
|
||||
|
||||
//image column
|
||||
sheet.SetColumnWidth(i, 16 * imageNames.Count * 256); //width: picture width(16:100), image total, per character(256)
|
||||
foreach (var ins in imageNames)
|
||||
{
|
||||
//image initial
|
||||
var extName = ins.Split('.')[1].ToUpper().Equals("JPG") ? "PNG" : ins.Split('.')[1].ToUpper();
|
||||
var fullPath = Path.Combine(operationFileSaveAsPath, ins);
|
||||
var img = Image.FromFile(fullPath);
|
||||
|
||||
//compress image
|
||||
decimal sizeRatio = ((decimal)img.Height / img.Width);
|
||||
int thumbWidth = 100;
|
||||
int thumbHeight = decimal.ToInt32(sizeRatio * thumbWidth);
|
||||
var thumbStream = img.GetThumbnailImage(thumbWidth, thumbHeight, () => false, IntPtr.Zero);
|
||||
var memoryStream = new MemoryStream();
|
||||
thumbStream.Save(memoryStream, ImageFormat.Jpeg);
|
||||
|
||||
//setting pictureType enum
|
||||
int format = 0;
|
||||
var drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();
|
||||
format = Convert.ToInt32(Enum.Parse(typeof(PictureType), extName));
|
||||
|
||||
//set picture size and location
|
||||
if (format != 0)
|
||||
{
|
||||
var pictureIds = workbook.AddPicture(memoryStream.ToArray(), format);
|
||||
XSSFClientAnchor anchor = new XSSFClientAnchor(XSSFShape.EMU_PER_PIXEL * dx1, XSSFShape.EMU_PER_PIXEL * dy1, 0, 0, i, j, 0, 0);
|
||||
anchor.AnchorType = AnchorType.DontMoveAndResize;
|
||||
var picture = (XSSFPicture)drawing.CreatePicture(anchor, pictureIds);
|
||||
var size = picture.GetImageDimension();
|
||||
row.HeightInPoints = size.Height;
|
||||
picture.Resize();
|
||||
|
||||
//add 105 each new pic
|
||||
dx1 += 105;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
j++;
|
||||
}
|
||||
if (i == 8)
|
||||
{
|
||||
@ -484,11 +536,13 @@ namespace FrontendWebApi.ApiControllers
|
||||
{
|
||||
var sqlString = $@"select opr.*, concat(d.device_floor_tag, ' ', d.device_last_name, ' ', d.device_serial_tag) as device_name, ui.full_name as user_full_name
|
||||
from operation_record opr
|
||||
left join device d on opr.fix_do_code = d.device_number
|
||||
left join userinfo ui on opr.work_person_id = ui.id
|
||||
left join device d on opr.fix_do_code = d.device_number and d.deleted = 0 and d.device_area_tag = SUBSTRING_INDEX(opr.fix_do_code, '_', 1)
|
||||
and d.device_building_tag = SUBSTRING_INDEX(opr.fix_do_code, '_', 2) and d.device_floor_tag = SUBSTRING_INDEX(opr.fix_do_code, '_', 5)
|
||||
and d.device_system_tag = opr.device_system_category_layer2 and d.device_name_tag = opr.device_system_category_layer3
|
||||
left join userinfo ui on opr.work_person_id = ui.userinfo_guid
|
||||
where opr.deleted = 0 and opr.work_type = 2";
|
||||
var sheet = workbook.CreateSheet("廠商資料");
|
||||
var operation_record = backendRepository.GetAllAsync<Operation_Record>(sqlString, null);
|
||||
var operation_record = backendRepository.GetAllAsync<Operation_Record>(sqlString);
|
||||
List<Operation_Record_File> operation_record_file = new List<Operation_Record_File>();
|
||||
|
||||
if (operation_record.Result.Count > 0)
|
||||
@ -548,13 +602,13 @@ namespace FrontendWebApi.ApiControllers
|
||||
cell = row.CreateCell(10);
|
||||
cell.SetCellValue("建立時間");
|
||||
cell.CellStyle = styleLine12;
|
||||
var j = 0;//row
|
||||
|
||||
#endregion
|
||||
foreach (var or in operation_record.Result)
|
||||
{
|
||||
RowPosition += 1;
|
||||
row = sheet.CreateRow(RowPosition);
|
||||
for (var i = 0; i < 10; i++)//column
|
||||
for (var i = 0; i < 11; i++)//column
|
||||
{
|
||||
cell = row.CreateCell(i);
|
||||
if (i == 0)
|
||||
@ -565,9 +619,10 @@ namespace FrontendWebApi.ApiControllers
|
||||
{
|
||||
cell.SetCellValue(or.location);
|
||||
}
|
||||
if (i == 1)
|
||||
if (i == 2)
|
||||
{
|
||||
cell.SetCellValue(or.error_code);
|
||||
sheet.AutoSizeColumn(i);
|
||||
}
|
||||
if (i == 3)
|
||||
{
|
||||
@ -587,31 +642,61 @@ namespace FrontendWebApi.ApiControllers
|
||||
}
|
||||
if (i == 7)
|
||||
{
|
||||
cell.SetCellValue(or.start_time);
|
||||
cell.SetCellValue(or.start_time.ToString("yyyy/MM/dd HH:mm:ss"));
|
||||
}
|
||||
if (i == 8)//image
|
||||
{
|
||||
var image = or.lorf.FirstOrDefault().save_file_name;
|
||||
var extName = "JPG"; //image.Split('.')[1];
|
||||
var fullPath = Path.Combine(operationFileSaveAsPath, "image1.jpg");
|
||||
var imageByte = System.IO.File.ReadAllBytes(fullPath);
|
||||
var imageNames = or.lorf.Count > 0 ? or.lorf.Select(x => x.save_file_name).ToList() : null;
|
||||
if (imageNames != null)
|
||||
{
|
||||
//insert image process
|
||||
var dy1 = 10;
|
||||
var dx1 = 10;
|
||||
|
||||
//image column
|
||||
sheet.SetColumnWidth(i, 16 * imageNames.Count * 256); //width: picture width(16:100), image total, per character(256)
|
||||
foreach (var ins in imageNames)
|
||||
{
|
||||
//image initial
|
||||
var extName = ins.Split('.')[1].ToUpper().Equals("JPG") ? "PNG" : ins.Split('.')[1].ToUpper();
|
||||
var fullPath = Path.Combine(operationFileSaveAsPath, ins);
|
||||
var img = Image.FromFile(fullPath);
|
||||
|
||||
//compress image
|
||||
decimal sizeRatio = ((decimal)img.Height / img.Width);
|
||||
int thumbWidth = 100;
|
||||
int thumbHeight = decimal.ToInt32(sizeRatio * thumbWidth);
|
||||
var thumbStream = img.GetThumbnailImage(thumbWidth, thumbHeight, () => false, IntPtr.Zero);
|
||||
var memoryStream = new MemoryStream();
|
||||
thumbStream.Save(memoryStream, ImageFormat.Jpeg);
|
||||
|
||||
//setting pictureType enum
|
||||
int format = 0;
|
||||
var drawing = sheet.CreateDrawingPatriarch();
|
||||
var drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch();
|
||||
format = Convert.ToInt32(Enum.Parse(typeof(PictureType), extName));
|
||||
|
||||
//set picture size and location
|
||||
if (format != 0)
|
||||
{
|
||||
var pictureIds = workbook.AddPicture(imageByte, format);
|
||||
XSSFClientAnchor anchor = new XSSFClientAnchor();
|
||||
anchor.Col1 = i;
|
||||
anchor.Row1 = j;
|
||||
var picture = drawing.CreatePicture(anchor, pictureIds);
|
||||
var pictureIds = workbook.AddPicture(memoryStream.ToArray(), format);
|
||||
XSSFClientAnchor anchor = new XSSFClientAnchor(XSSFShape.EMU_PER_PIXEL * dx1, XSSFShape.EMU_PER_PIXEL * dy1, 0, 0, i, j, 0, 0);
|
||||
anchor.AnchorType = AnchorType.DontMoveAndResize;
|
||||
var picture = (XSSFPicture)drawing.CreatePicture(anchor, pictureIds);
|
||||
var size = picture.GetImageDimension();
|
||||
row.HeightInPoints = size.Height;
|
||||
picture.Resize();
|
||||
|
||||
//add 105 each new pic
|
||||
dx1 += 105;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
j++;
|
||||
}
|
||||
if (i == 9)
|
||||
{
|
||||
cell.SetCellValue(or.finish_time);
|
||||
cell.SetCellValue(or.finish_time.ToString("yyyy/MM/dd HH:mm:ss"));
|
||||
}
|
||||
if (i == 10)
|
||||
{
|
||||
@ -619,7 +704,7 @@ namespace FrontendWebApi.ApiControllers
|
||||
}
|
||||
|
||||
cell.CellStyle = style12;
|
||||
j++;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -633,7 +718,7 @@ namespace FrontendWebApi.ApiControllers
|
||||
ms.Flush();
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
return File(ms, "application/vnd.ms-excel", "廠商資料.xlsx");
|
||||
return File(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "廠商資料.xlsx");
|
||||
}
|
||||
|
||||
#region 廠商
|
||||
|
Loading…
Reference in New Issue
Block a user