编程(C#)转换Excel中的图像 [英] Programmatically (C#) convert Excel to an image

查看:184
本文介绍了编程(C#)转换Excel中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要到Excel文件转换为图像(每格式是确定)编程(C#)。目前我使用微软互操作的图书馆及放大器; 。Office 2007中,但它不支持的默认保存到图片

I want to convert an excel file to an image (every format is ok) programmatically (c#). Currently I'm using Microsoft Interop Libraries & Office 2007, but it does not support saving to an image by default.

所以,我目前的解决方法如下:

So my current work-around is as follows:


  • 开启Excel文件;

  • 找到了最大范围(包含数据);

  • 在该范围内使用CopyPicture(),将数据复制到剪贴板。

现在棘手的部分(和我的问题):

Now the tricky part (and my problems):

问题1:

使用.NET Clipboard类,我无法从剪贴板中得到确切的复制数据:数据是一样的,但不知何故格式是扭曲的(整个文档的字体似乎变得大胆,而他们没有一点点不可读);如果我使用mspaint.exe剪贴板粘贴,粘贴的图像是正确的(而且就像我希望它是)。

Using the .NET Clipboard class, I'm not able to get the EXACT copied data from the clipboard: the data is the same, but somehow the formatting is distorted (the font of the whole document seems to become bold and a little bit more unreadable while they were not); If I paste from the clipboard using mspaint.exe, the pasted image is correct (and just as I want it to be).

我拆开mspaint.exe,发现它是用(OleGetClipboard),以获得从剪贴板数据的功能,但我似乎无法得到它在C#/工作。 NET。

I disassembled mspaint.exe and found a function that it is using (OleGetClipboard) to get data from the clipboard, but I cannot seem to get it working in C# / .NET.

其他的事情我想是剪贴板WINAPI的(OpenClipboard,GetClipboardData,CF_ENHMETAFILE),但结果是一样的使用.NET版本。

Other things I tried were the Clipboard WINAPI's (OpenClipboard, GetClipboardData, CF_ENHMETAFILE), but the results were the same as using the .NET versions.

问题2:

使用范围和CopyPicture,如果有在Excel中的任何图像板材,这些图像不与周围的数据复制到剪贴板一起复制。

Using the range and CopyPicture, if there are any images in the excel sheet, those images are not copied along with the surrounding data to the clipboard.

部分的源代码

Excel.Application app = new Excel.Application();
app.Visible = app.ScreenUpdating = app.DisplayAlerts = false;
app.CopyObjectsWithCells = true;
app.CutCopyMode = Excel.XlCutCopyMode.xlCopy;
app.DisplayClipboardWindow = false;

try {
    Excel.Workbooks workbooks = null;
    Excel.Workbook book = null;
    Excel.Sheets sheets = null;

    try {
    	workbooks = app.Workbooks;
    	book = workbooks.Open(inputFile, false, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
    						  Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
    						  Type.Missing, Type.Missing);
    	sheets = book.Worksheets;
    } catch {
    	Cleanup(workbooks, book, sheets);	//Cleanup function calls Marshal.ReleaseComObject for all passed objects
    	throw;
    }

    for (int i = 0; i < sheets.Count; i++) {
    	Excel.Worksheet sheet = (Excel.Worksheet)sheets.get_Item(i + 1);

    	Excel.Range myrange = sheet.UsedRange;
    	Excel.Range rowRange = myrange.Rows;
    	Excel.Range colRange = myrange.Columns;

    	int rows = rowRange.Count;
    	int cols = colRange.Count;

    	//Following is used to find range with data
    	string startRange = "A1";
    	string endRange = ExcelColumnFromNumber(cols) + rows.ToString();

    	//Skip "empty" excel sheets
    	if (startRange == endRange) {
    		Excel.Range firstRange = sheet.get_Range(startRange, endRange);
    		Excel.Range cellRange = firstRange.Cells;
    		object text = cellRange.Text;
    		string strText = text.ToString();
    		string trimmed = strText.Trim();

    		if (trimmed == "") {
    			Cleanup(trimmed, strText, text, cellRange, firstRange, myrange, rowRange, colRange, sheet);
    			continue;
    		}
    		Cleanup(trimmed, strText, text, cellRange, firstRange);
    	}

    	Excel.Range range = sheet.get_Range(startRange, endRange);
    	try {
    		range.CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlPicture);

    		//Problem here <-------------
    		//Every attempt to get data from Clipboard fails
    	} finally {
    		Cleanup(range);
    		Cleanup(myrange, rowRange, colRange, sheet);
    	}
    }   //end for loop

    book.Close(false, Type.Missing, Type.Missing);
    workbooks.Close();

    Cleanup(book, sheets, workbooks);
} finally {
    app.Quit();
    Cleanup(app);
    GC.Collect();
}



使用WINAPI剪贴板获取数据成功,但质量不好。来源:

Getting data from the clipboard using WINAPI succeeds, but with bad quality. Source:

protected virtual void ClipboardToPNG(string filename) {
    if (OpenClipboard(IntPtr.Zero)) {
        if (IsClipboardFormatAvailable((int)CLIPFORMAT.CF_ENHMETAFILE)) {
            int hEmfClp = GetClipboardDataA((int)CLIPFORMAT.CF_ENHMETAFILE);

            if (hEmfClp != 0) {
                int hEmfCopy = CopyEnhMetaFileA(hEmfClp, null);

                if (hEmfCopy != 0) {
                    Metafile metafile = new Metafile(new IntPtr(hEmfCopy), true);

                    metafile.Save(filename, ImageFormat.Png);
                }
            }
        }

        CloseClipboard();
    }
}



任何人都得到一个解决方案? (我使用.NET 2.0的BTW)

Anyone got a solution? (I'm using .NET 2.0 btw)

推荐答案

的SpreadsheetGear的.NET 将做到这一点。

您可以看到我们的ASP.NET(C#和VB) Excel图表和范围成像样品样品这里和下载免费审判这里如果你想尝试一下。

You can see our ASP.NET (C# and VB) "Excel Chart and Range Imaging Samples" samples here and download a free trial here if you want to try it out.

的SpreadsheetGear也适用于Windows窗体,控制台应用程序,等等...(你没有指定要创建的应用程序类型)。 。还有一个Windows窗体控件来显示应用程序的工作簿,如果这是你的真实意图

SpreadsheetGear also works with Windows Forms, console applications, etc... (you did not specify what type of application you are creating). There is also a Windows Forms control to display a workbook in your application if that is what you are really after.

免责声明:我自己的SpreadsheetGear LLC

Disclaimer: I own SpreadsheetGear LLC

这篇关于编程(C#)转换Excel中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆