C# 把excell 轉換成圖片

來源:互聯網
上載者:User
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 釋放資源
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,拷貝數據到粘貼板中,然後把粘貼板中的數據,儲存成圖片

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.