現(xiàn)在的網(wǎng)站設(shè)計前端seo主要優(yōu)化哪些
????????今天在本文中,我們將嘗試使用NPOI庫將圖像插入到 Excel 文件的特定位置。請將以下邏輯添加到您的寫作方法中,在 Excel 文件中添加圖像(JPEG、PNG),我已經(jīng)有一個示例 jpeg 文件 - Read-write-excel-npoi.jpg ,我們將嘗試將其插入索引 (5,5),即第 5 行和第 5 列。
在第 5 行和第 5 列,將以編程方式插入上述圖像,代碼如下:
????????????????byte[] data = File.ReadAllBytes("Read-write-excel-npoi.jpg");//根據(jù)自己路徑讀取圖片
? ? ? ? ? ? ? ? int pictureIndex = workbook.AddPicture(data, PictureType.JPEG);
? ? ? ? ? ? ? ? ICreationHelper helper = workbook.GetCreationHelper();
? ? ? ? ? ? ? ? IDrawing drawing = excelSheet.CreateDrawingPatriarch();
? ? ? ? ? ? ? ? IClientAnchor anchor = helper.CreateClientAnchor();
? ? ? ? ? ? ? ? anchor.Col1 = 5;
? ? ? ? ? ? ? ? anchor.Row1 = 5;
? ? ? ? ? ? ? ? IPicture picture = drawing.CreatePicture(anchor, pictureIndex);
? ? ? ? ? ? ? ? picture.Resize();?
用圖像寫入 EXCEL
下面是一個 POC 完整代碼示例,如下所示:?
static void WriteExcel()
? ? ? ?{
? ? ? ? ? ?List<UserDetails> persons = new List<UserDetails>()
? ? ? ? ? ?{
? ? ? ? ? ? ? ?new UserDetails() {ID="1001", Name="ABCD", City ="City1", Country="USA"},
? ? ? ? ? ? ? ?new UserDetails() {ID="1002", Name="PQRS", City ="City2", Country="INDIA"},
? ? ? ? ? ? ? ?new UserDetails() {ID="1003", Name="XYZZ", City ="City3", Country="CHINA"},
? ? ? ? ? ? ? ?new UserDetails() {ID="1004", Name="LMNO", City ="City4", Country="UK"},
? ? ? ? ? };
?
? ? ? ? ? ?// Lets converts our object data to Datatable for a simplified logic.
? ? ? ? ? ?// Datatable is most easy way to deal with complex datatypes for easy reading and formatting.?
? ? ? ? ? ?DataTable table = (DataTable)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(persons), (typeof(DataTable)));
?
?
? ? ? ? ? ?var memoryStream = new MemoryStream();
?
? ? ? ? ? ?using (var fs = new FileStream("Result.xlsx", FileMode.OpenOrCreate, FileAccess.Write))
? ? ? ? ? ?{
? ? ? ? ? ? ? ?IWorkbook workbook = new XSSFWorkbook();
? ? ? ? ? ? ? ?ISheet excelSheet = workbook.CreateSheet("TestSheet1");
?
? ? ? ? ? ? ? ?List<String> columns = new List<string>();
? ? ? ? ? ? ? ?IRow row = excelSheet.CreateRow(0);
? ? ? ? ? ? ? ?int columnIndex = 0;
?
? ? ? ? ? ? ? ?foreach (System.Data.DataColumn column in table.Columns)
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?columns.Add(column.ColumnName);
? ? ? ? ? ? ? ? ? ?row.CreateCell(columnIndex).SetCellValue(column.ColumnName);
? ? ? ? ? ? ? ? ? ?columnIndex++;
? ? ? ? ? ? ? ?}
?
? ? ? ? ? ? ? ?int rowIndex = 1;
? ? ? ? ? ? ? ?foreach (DataRow dsrow in table.Rows)
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?row = excelSheet.CreateRow(rowIndex);
? ? ? ? ? ? ? ? ? ?int cellIndex = 0;
? ? ? ? ? ? ? ? ? ?foreach (String col in columns)
? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ?row.CreateCell(cellIndex).SetCellValue(dsrow[col].ToString());
? ? ? ? ? ? ? ? ? ? ? ?cellIndex++;
? ? ? ? ? ? ? ? ? ?}
?
? ? ? ? ? ? ? ? ? ?rowIndex++;
? ? ? ? ? ? ? ?}
?
? ? ? ? ? ? ? ?byte[] data = File.ReadAllBytes("Read-write-excel-npoi.jpg");
? ? ? ? ? ? ? ?int pictureIndex = workbook.AddPicture(data, PictureType.JPEG);
?? ??? ??? ? ? ICreationHelper helper = workbook.GetCreationHelper();
? ? ? ? ? ? ? ?IDrawing drawing = excelSheet.CreateDrawingPatriarch();
? ? ? ? ? ? ? ?IClientAnchor anchor = helper.CreateClientAnchor();
? ? ? ? ? ? ? ?anchor.Col1 = 5;
? ? ? ? ? ? ? ?anchor.Row1 = 5;
? ? ? ? ? ? ? ?IPicture picture = drawing.CreatePicture(anchor, pictureIndex);
? ? ? ? ? ? ? ?picture.Resize();
?
? ? ? ? ? ? ? ?workbook.Write(fs);
? ? ? ? ? ?}
? ? ? ?}?
????????我將圖像文件保存在同一個項目目錄中,以便 Excel API 可以使用它并將其加載到 Excel 中的正確位置。最后圖像將成功輸入到所需位置:
如果您喜歡此文章,請收藏、點贊、評論,謝謝,祝您快樂每一天。