求解,沒辦法,轉不成功
以下我的code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EXCEL = Microsoft.Office.Interop.Excel;
using System.IO;
namespace _5_23_excel__to_jpg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// 
/// Excel文檔Jpg圖片
/// 
public class ConvertEXCEL
{
/// 
/// 圖片保存路徑。System.IO.Path.GetTempPath() + @"cache{0}.jpg"
/// 
private static string SAVEEXCELJPG = @"D:\DocToImg\666{0}.jpg";
/// 
/// 暫時網頁保存文件路徑
/// Path.GetTempPath() + @"cache{0}.htm"
/// 
private static string TEMPHTMLPATH = @"D:\DocToImg\666\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;
                    //設置選擇單元格,在複製出來,Excel轉換成圖片的操作方法
                    wsheet.get_Range("A1", "D3").Copy(ranobj);
                    //全選單元格,全部複製出来。Excel轉換成圖片的操作方法。
                  
                    //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);
        }
    }
}
}
參考:
http://www.cnblogs.com/lyl6796910/archive/2013/01/24/2875799.html

好吧,
既然你誠心誠意的問了,
我就大發慈悲的告訴你,
大概改兩三個地方就可以了,
可以結合allenlwh貼出來的,
基本上功能差不多,
缺點就是一個工作表只印一頁...
這部分找時間再來想辦法吧...
public static string GetExcel(string filePath, string outPutPath)
{
    EXCEL.Application app = new Microsoft.Office.Interop.Excel.Application();
    object objMis = Type.Missing;
    EXCEL.Workbook singleExcel = app.Workbooks.Open(filePath, 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;
            //设置选择单元格,在复制出来。Excel转换成图片的操作方法。
            wsheet.get_Range("A1", "D3").Copy(ranobj);
            //全选单元格,全部复制出来。Excel转换成图片的操作方法。
            //wsheet.UsedRange.Copy(objMis);
            //Clipboard.SetDataObject(objMis);
            System.Windows.Forms.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($"{outPutPath}test_{i}.jpg", ImageFormat.Jpeg);
            Clipboard.Clear();
            myBitmap.Dispose();
            bits.Dispose();
        }
    }
    catch (Exception Excel)
    {
        throw Excel;
    }
    finally
    {
        singleExcel.Close(objMis, objMis, objMis);
        app.Quit();
    }
    return "輸出成功!";
}
剛才那是Function,下面是使用方法
string result = GetExcel(@"D:\test.xls", @"D:\tmp\");
label1.Text = result;
另外,這種方式會開啟文件(只是沒讓你看到畫面),
所以要開.xls檔案就要安裝Office 2003左右版本,
要開.xlsx檔案就要安裝Office 2007之後的版本。
wsheet.get_Range("A1", "D3").Copy(ranobj);
這一行是設定列印範圍,
所以只有一小部分被印出來,
如果要印全部範圍就要改成
wsheet.UsedRange.Copy(objMis);
不過如果資料很多印出來字就會很小,
因為要把所有資料印在一頁...
不過只差這麼一點點,
你竟然沒辦法自己解決,
自己解決問題應該是程式設計師的基本功吧...
小魚大大 謝謝你分享code,那我也分享我昨天 有突然試成功的code
static void Main(string[] args)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"D:\DocToImg\555.xlsx", ExcelVersion.Version97to2003);
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToImage("D:\DocToImg\555.png");
這個也能轉換檔案...成圖檔功能
但小弟還在思考 怎麼去調它的解析度
這個方法我倒是沒試成功,
你也是用 Microsoft.Office.Interop.Excel; 套件嗎?
你再重新開一個專案,直接複製貼上code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Xls;
using System.Drawing.Imaging;
using System.Drawing;
using 自這
然後參考   Microsoft.Office.Interop.Excel;  我有加
Spire.Xls 這哪來的?
提供之前的做法(C# 2008),給你參考
1.設定excel輸出範圍
myRange = xlWorkSheet.get_Range("A1", "O19");
2.複製到剪貼簿
myRange.CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlBitmap);
3.將剪貼簿內的圖片存到物件內
Image image = (Image)data.GetData(DataFormats.Bitmap, true);
this.pictureBox1.Image = image;
4.存檔
image.Save(Exceljpg, System.Drawing.Imaging.ImageFormat.Jpeg);