天天看點

c#往excel裡寫内容,包括圖檔

第一種方式InsertPicToExcel類的位址:https://blog.csdn.net/zhu58252601/article/details/101026675

#region  //第一種方式
        //InsertPicToExcel ipt = new InsertPicToExcel();
        //ipt.Open();
        //ipt.InsertPicture("B2", @"F:\1.png");
        //ipt.InsertPicture("B18", @"F:\1.png", 100, 80);
        //ipt.SaveFile(@"F:\excel1.xlsx");
        //ipt.Dispose();
        #endregion


        #region //第二種方式 
        //建立Applicaton對象
        //Excel.Application xApp = new Excel.Application();

        //xApp.Visible = true;

        //#region 打開excel文檔
        得到workbook對象,可以用兩種方式之一:下面是打開已有的excel檔案
        //Excel.Workbook xBook = xApp.Workbooks.Open(@"F:\excel.xlsx",
        //                       Missing.Value, Missing.Value, Missing.Value, Missing.Value,
        //                       Missing.Value, Missing.Value, Missing.Value, Missing.Value,
        //                       Missing.Value, Missing.Value, Missing.Value, Missing.Value);

        //#endregion

        指定要操作的Sheet,兩種方式:
        //Excel.Worksheet xSheet = xBook.Sheets[1];
        Excel.Worksheet xSheet = xApp.ActiveSheet;

        讀取資料,通過Range對象
        //Excel.Range rng1 = xSheet.get_Range("A1", Type.Missing);
        //if (rng1.Value2 != null)
        //    Console.WriteLine(rng1.Value2);

        讀取資料,通過Range對象,但使用不同的接口得到Range
        //Excel.Range rng2 = (Excel.Range)xSheet.Cells[1, 3];  //得到第1行第3列的值 cells[行,列]
        //if (rng2.Value2 != null)
        //    Console.WriteLine(rng2.Value2);

        動态寫入資料,将資料寫入到不同的單元格
        //int i = 0;
        //for (i = 1; i < 10; i++)
        //{
        //    Excel.Range rng4 = (Excel.Range)xSheet.Cells[2, i];
        //    rng4.Value2 = i;
        //    rng4.Interior.ColorIndex = 6; //裝置Range的背景色
        //} 
        儲存  :
        //xBook.Save();

        //xSheet = null;
        //xBook = null;
        //xApp.Quit();    //這一句非常重要,否則Excel對象不能從記憶體中退出
        //xApp = null;

        //Console.ReadKey();
        #endregion


        #region//第三種方式

        //IWorkbook workbook = new HSSFWorkbook();
        //ISheet sheet = workbook.CreateSheet("Sheet1");
        //IDrawing patriarch = sheet.CreateDrawingPatriarch();

        //string setVal = @"F:\1.png";
        将圖檔檔案讀入一個字元串
        //byte[] bytes = System.IO.File.ReadAllBytes(setVal);

        //int pictureIdx = sheet.Workbook.AddPicture(bytes, PictureType.JPEG);

        //IClientAnchor anchor = null;
        //IPicture pict = null;

        //int dx1 = 0, dy1 = 0, dx2 = 1023, dy2 = 255;
        //int col1 = 1, row1 = 1, col2 = 1, row2 = 1;
        圖檔可能會合并多個單元格,需要計算合并的跨度
        //if (sheet.GetRow(row1).GetCell(col1) != null && sheet.GetRow(row1).GetCell(col1).IsMergedCell)
        //{
        //    int rowSpan = 0;
        //    int colSpan = 0;
        //    int regionsCount = sheet.NumMergedRegions;
        //    for (int i = 0; i < regionsCount; i++)
        //    {
        //        CellRangeAddress range = sheet.GetMergedRegion(i);
        //        sheet.IsMergedRegion(range);
        //        if (range.FirstRow == row1 && range.FirstColumn == col1)
        //        {
        //            rowSpan = range.LastRow - range.FirstRow + 1;
        //            colSpan = range.LastColumn - range.FirstColumn + 1;
        //            break;
        //        }
        //    }

        //    col2 = col1 + colSpan - 1;
        //    row2 = row1 + rowSpan - 1;
        //}


        //anchor = new HSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2);
        把圖檔插到相應的位置
        //pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
        #endregion

        #region//第四種方式,用epplus
        FileInfo file = new FileInfo(@"D:\myExcel1.xlsx");
         
        ExcelPackage package = new ExcelPackage(file);
        Image logo = Image.FromFile(@"F:\1.png");
        var ws = package.Workbook.Worksheets[1];
        //int maxRowNum = ws.Dimension.End.Row;//最大行 
        for (int a = 0; a < 1; a++)
        {
            ws.Row(a * 5).Height = 39.00D;
            var picture = ws.Drawings.AddPicture(a.ToString(), logo);
            picture.SetPosition(a * 5, 0, 2, 0);
            picture.EditAs=OfficeOpenXml.Drawing.eEditAs.TwoCell;//設定圖檔随單元格變動
        }
        package.Save();
        #endregion