程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 把excell 轉換成圖片

C# 把excell 轉換成圖片

編輯:C#入門知識

public  class CoverExcellToImage
    {
        private static string SAVEEXCELJPG = @"c:/temp/{0}.jpg";
        private static readonly string[] ArrRang = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

      public static event TECOSFIS.SFIS.UpdateImage.ShowMessage eventShowMessage;
      public static string StrRouseFile = "";
      public static List<string> ListFileName = new List<string>();

      public static string GetExcel(string excelFilePath)
      {
          if (!System.IO.Directory.Exists("c:/temp")) {

              System.IO.Directory.CreateDirectory("c:/temp");
          }

          Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
          app.DisplayAlerts = false;
          object objMis = Type.Missing;
          Microsoft.Office.Interop.Excel.Workbook singleExcel = app.Workbooks.Open(excelFilePath, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis);
          try
          {
              StrRouseFile = excelFilePath;
              //wsheet.UsedRange.Select();
              if (eventShowMessage != null)
              {
                  eventShowMessage(1, singleExcel.Worksheets.Count);

              }
              for (int i = 1; i <= singleExcel.Worksheets.Count; i++)
              {
                  Microsoft.Office.Interop.Excel.Worksheet wsheet = (Microsoft.Office.Interop.Excel.Worksheet)singleExcel.Worksheets[i];

                  ListFileName.Add(wsheet.Name);
                  object ranobj = DBNull.Value;

                  //設置選擇單元格,在復制出來。
                  int iRows =36;
                  int iCols = 8;
#warning 這裡需要修改的內容
                  wsheet.get_Range("A5", ArrRang[iCols] + iRows.ToString()).Copy(ranobj);
                  IDataObject iData = Clipboard.GetDataObject();
                  Bitmap bits = (Bitmap)iData.GetData(DataFormats.Bitmap);
                  Bitmap myBitmap = new Bitmap(bits.Width, bits.Height);
                  Graphics g = Graphics.FromImage(myBitmap);
                  g.DrawImage(bits, 0, 0);

                  myBitmap.Save(string.Format(SAVEEXCELJPG, wsheet.Name));
                  Clipboard.SetDataObject("");
                  myBitmap.Dispose();
                  bits.Dispose();
                  if (wsheet != null)
                  {
                      System.Runtime.InteropServices.Marshal.ReleaseComObject(wsheet);
                      wsheet = null;
                  }
                  if (eventShowMessage != null)
                  {
                      eventShowMessage(2, i);

                  }
              }

          }
          catch (Exception Excel)
          {
              throw Excel;
          }
          finally
          {
              #region 釋放資源www.2cto.com
              singleExcel.Close(null, null, null);
              app.Workbooks.Close();
              app.Quit();
              if (singleExcel != null)
              {
                  System.Runtime.InteropServices.Marshal.ReleaseComObject(singleExcel);
                  singleExcel = null;
              }
              if (app != null)
              {
                  System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
                  app = null;
              }
              GC.Collect();
              #endregion
          }
          return string.Empty;
      }

  }
原理:
       讀取 excell文件中每一個 sheet,拷貝數據到粘貼板中,然後把粘貼板中的數據,保存成圖片

 

摘自 無限遐想
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved