C # compile excell into a picture

Source: Internet
Author: User

Public class CoverExcellToImage
{
Private static string SAVEEXCELJPG = @ "c:/temp/10902.16.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 );
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;

// Set the selected cell and copy it out.
Int iRows = 36;
Int iCols = 8;
# Contents to be modified in 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 releases resources www.2cto.com
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;
}

}
Principle:
Copy each sheet in the excell file to the clipboard, and then save the data in the clipboard as a chip.

 

From unlimited imagination
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.