發表文章

目前顯示的是有「NPOI」標籤的文章

Excel 下拉選單

public void GenerateWithValidation<T>(string TemplatePath, string ReportPath, List<T> entities, int Offset, int PageSize) { using (FileStream fileStream = new FileStream(TemplatePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { IWorkbook workbook = WorkbookFactory.Create(fileStream); ISheet sheet = workbook.GetSheetAt(0); List<ICell> TemplateCells = new List<ICell>(); for (int i = 0; i < sheet.GetRow(Offset).Cells.Count; i++) { TemplateCells.Add(sheet.GetRow(Offset).GetCell(i)); } PropertyInfo[] properties = typeof(T).GetProperties(); foreach (var entity in entities) { sheet.CreateRow(Offset); int CellInRow = 0; foreach (var property in properties) { ICell cell = sheet.GetRow(Offset).CreateCell(CellInRow); cell.CellStyle = TemplateCells[CellInRow].CellStyle; cel...

.NET 建立加密ZIP

 之前有一篇是  走訪資料夾及Web下載檔案(zip)  這篇 主要使用的是  ZipArchive   System.IO.Compression 名命空間裡提供一個類別 ZipArchive 但是這個類別有一個問題 那就是無法加密。 接著因為工作需求,老闆叫我搞一個加密的,因此有了這篇文章 主要還是依照  走訪資料夾及Web下載檔案(zip)  的概念 但是這次使用的是DotNetZip static void Main(string[] args)         {             //目前抓前一天 可修改             DateTime Start = DateTime.Parse(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00.000"));             DateTime End = DateTime.Parse(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 23:59:59.999"));             List<model> lsResponse = ModelData();             string nasPath = ConfigurationManager.AppSettings["nasPath"];             string xmlPath = ConfigurationManager.AppSettings["xmlPath"];             //目前數量 控管在2000內        ...

NPOI 匯入圖片進入Excel

{ double ColWidth = 540.0; Func<IPicture, (double width, double height)> GetImageDimensions = (pict) => {     try     {         return (pict?.GetImageDimension().Width ?? 0, pict?.GetImageDimension().Height ?? 0);     }     catch (Exception ex)     {         return (0, 0);     } };  byte[] buffer = File.ReadAllBytes(path); //建立 pic int picIdx = workbook.AddPicture(buffer, PictureType.PNG); //建立 畫圖區 var drawing = sheet.CreateDrawingPatriarch(); //確認方位 偏移x1 y1 x2 y2 建立在 colx1 rowy1 colx2 rowy2  XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 0, i, 10, i + 1); //確認位置後建立圖片 var pict = drawing.CreatePicture(anchor, picIdx); //取得原始長寬 這段要用來縮放圖片 var ImageSize = GetImageDimensions(pict); //catch error 回0 if (ImageSize.width == 0 || ImageSize.height == 0) {     _logger.LogInformation(string.Format($"匯出-完工報告 圖片來源 : {0} , 匯入失敗", list));     continue; }...

Excel 插入圖片設定

Excel插入圖片設定         public Task<string>  InsertPicToExcel(string path = "圖片檔名.jpg") { #region Func 用於調整圖片大小的function 這段在這邊的原因是因為當初寫的地方是API 無法額外寫function Func<string, (int width, int height)> GetImageDimensions = (path) => {     if (!File.Exists(path))     {         Console.WriteLine("找不到圖片");         return (-1, -1);     }     System.Drawing.Image image = System.Drawing.Image.FromFile(path);     return (image.Width, image.Height); }; #endregion  //取得原始長寬 var ImageSize = GetImageDimensions (list); //長寬比例 double Scale = 1.0; //依照寬度進行圖片調整 小的拉長 大的縮小 // 當初圖片大小 890 = 23.55cm  // 因此欄位需求大小為15.5的話 求得值為 = 540 // 取得比例 Scale = 540.0 / ImageSize.width; //依照圖片高度建立Row //這邊因為 XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 0, i, 10, i + 1); 這段 因此會自動成為圖片的寬度 //簡單來講 把任意圖片拉為 540 x (Y * 540 / ImageSize.width) 大小 row.Height = Convert.ToInt16(ImageSize.height *...

//新的Excel寫法#2

 //新的Excel寫法#2 public Task<string> CommonWriteExcel<TData>(List<TData> dataList,Dictionary<string,string>reportHeader) {     var dataObject = JsonConvert.SerializeObject(dataList);     DataTable table = JsonConvert.DeserializeObject<DataTable>(dataObject);     using( var ms = new MemoryStream())     {         IWorkbook workbook = new XSSFWorkbook();         ISheet excelSheet = workbook.CreateSheet("Sheet1");         List<String> columns = new List<string>();         IRow row = excelSheet.CreateRow(0);         int columnIndex = 0;         foreach (System.Data.DataColumn column in table.Columns)         {             if (reportHeader.Keys.Contains(column.ColumnName))             {  ...

//新的Excel寫法#1

 //新的Excel寫法#1 public void ExcelCreate<T>(List<T> lsT , string savePath, XSSFWorkbook workbook = null, string SheetName = "預設頁面", string DataName = null             , bool overwrite = false)         {             //檢查workbook null則建立             if (workbook == null){                 workbook = new XSSFWorkbook();                 workbook.CreateSheet(SheetName);             }             //複寫 => 刪除後新增             if (overwrite == true)                 workbook.RemoveName(SheetName);             //若 Sheet為null 建立CreateSheet             if (workbook.GetSheet(SheetName) == null)     ...

判斷檔案是否存在、開檔寫及存Excel以及特殊存取ajax回傳值

public ActionResult Query(HttpPostedFileBase[] files)         {           //取得路徑 路徑為IIS的根目錄下的Temp 不管發佈在哪             string path = HttpContext.Server.MapPath("../Temp/");             //判斷是否存在資料夾,不存在則建立             if (!System.IO.Directory.Exists(path))             {                 System.IO.Directory.CreateDirectory(path);             }             //走訪回傳資料             try             {                 foreach (HttpPostedFileBase file in files)                 {                     //獲取Path,取IIS根目錄,並串起新newPath       ...

C# NPOI 匯入EXCEL

C# Workbook create string xlsName = "name.xls"; //Excel的名稱 //string filePath = HttpContext.Current.Server.MapPath("ExcelSample/") + xlsName; //路徑 //HSSFWorkbook workbook = csExcel.ReadSampleExcel(filePath);  //傳回Excel底檔 //底檔意思是先畫好的Excel 表格 HSSFWorkbook workbook = new HSSFWorkbook(); ISheet u_Sheet = (ISheet)workbook.CreateSheet("sheetName");//sheet 名稱 u_Sheet.DefaultColumnWidth = 11; //預設欄位寬度 C# NPOI Style //Create a CellStyle for Cell HSSFCellStyle csStringStyle = (HSSFCellStyle)workbook.CreateCellStyle(); //Border 粗細 分為下左右上 csStringStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; csStringStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; csStringStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; csStringStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; //Border 顏色 也分為上下左右 csStringStyle.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index; csStringStyle.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index; csStringStyle.RightBor...