• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#把excell轉換成圖片

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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,拷貝數據到粘貼板中,然後把粘貼板中的數據,保存成圖片


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap