using System;
using System.Collections.Generic;using System.Linq;using System.Text;using EXCEL = Microsoft.Office.Interop.Excel;using System.Windows.Forms;using System.Drawing;using System.IO;namespace FormatApplication.CommonClass
{ /// <summary> /// EXCEL文档JPG图片 /// </summary> public class ConvertEXCEL { /// <summary> /// 图片保存路径。System.IO.Path.GetTempPath() + @"cache\{0}.jpg" /// </summary> private static string SAVEEXCELJPG = @"E:\cheng_Text\FormatApplication\测试文件\{0}.jpg";/// <summary>
/// 暂时网页保存文件路径。 /// Path.GetTempPath() + @"cache\{0}.htm" /// </summary> private static string TEMPHTMLPATH = @"E:\cheng_Text\FormatApplication\测试文件\Excel.htm";/// <summary>
/// Excel文档转换成JPG图片文件。 /// </summary> /// <param name="excelPath">Excel文件路径</param> /// <returns>JPG图片路径</returns> public static string EXCELConvertImage(string excelPath) { EXCEL.Application appExcel = new Microsoft.Office.Interop.Excel.Application(); appExcel.Visible = false; object obj = Type.Missing; object fileHtml = TEMPHTMLPATH; object format = EXCEL.XlFileFormat.xlHtml; EXCEL.Workbook xls = appExcel.Workbooks.Open(excelPath, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj);xls.SaveAs(fileHtml, format, obj, obj, obj, obj, EXCEL.XlSaveAsAccessMode.xlExclusive, obj, obj, obj, obj, obj);
appExcel.Quit(); string HTMLImage = ConvertHTML.HTMLConvertImage(fileHtml.ToString()); DeleteTempFile(fileHtml.ToString()); return HTMLImage; }public static string GetExcel(string excelFilePath)
{ EXCEL.Application app = new Microsoft.Office.Interop.Excel.Application(); object objMis = Type.Missing; EXCEL.Workbook singleExcel = app.Workbooks.Open(excelFilePath, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis, objMis); try { //wsheet.UsedRange.Select(); for (int i = 1; i <= singleExcel.Worksheets.Count; i++) { EXCEL.Worksheet wsheet = (EXCEL.Worksheet)singleExcel.Worksheets[i];//Clipboard.Clear();
object ranobj = DBNull.Value;
//设置选择单元格,在复制出来。
wsheet.get_Range("A1", "D3").Copy(ranobj);//全选单元格,全部复制出来。
//wsheet.UsedRange.Copy(objMis); //Clipboard.SetDataObject(objMis); 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, Guid.NewGuid()));Clipboard.Clear();
myBitmap.Dispose(); bits.Dispose(); }}
catch (Exception Excel) { throw Excel; } finally { singleExcel.Close(objMis, objMis, objMis); app.Quit(); } return string.Empty; }/// <summary>
/// 删除指定的临时文件。 /// </summary> /// <param name="filePath"></param> private static void DeleteTempFile(string filePath) { File.Delete(filePath); } }}