Excel column width

Source: Internet
Author: User

Http://blog.csdn.net/ljl_xyf/article/details/6082534

 

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using Microsoft. Office. InterOP. Excel;
Using system. collections;
Using system. diagnostics;
/// <Summary>
/// Summary description for excelrw
/// </Summary>
Public static class excelrw
{

Public static void excelsave (hashtable HT)
{
Application Excel = new application (); // reference an Excel Object
Workbooks obooks;
Workbook obook;
Sheets osheets;
Worksheet osheet;
Range ocells;
String sfile = HT ["excelname"] as string;
// Excel. Save ("ddd.xls ");
Arraylist alsheet = HT ["sheetnamelist"] As arraylist;
Workbook WB = excel. workbooks. Add (true); // reference an Excel Workbook
Excel. Visible = false; // visualize Excel
Obooks = excel. workbooks;
Obook = obooks. get_item (1 );
Osheets = obook. worksheets;
Osheet = (worksheet) osheets. get_item (osheets. Count );
Foreach (hashtable htonesheel in alsheet)
{

Osheet = (worksheet) osheets. get_item (osheets. Count );
Arraylist colnamelist = htonesheel ["colname"] As arraylist;
// Name the sheet
Osheet. Name = htonesheel ["sheetname"] as string;
System. Data. datatable dtable = htonesheel ["datarows"] As system. Data. datatable;
Range column = (range) osheet. cells [1, 1]). entirecolumn;
Column. columnwidth = 10;
Int irow = 1;
Foreach (datarow DR in dtable. Rows)
{
Int ICEL = 1;
For (INT icol = 0; icol <colnamelist. Count; icol ++)
{
Osheet. cells [irow, ICEL ++] = convert. tostring (Dr [colnamelist [icol]. tostring ()]);

}
Irow ++;
}
Osheets. Add (type. Missing, osheets [osheets. Count], 1, type. Missing );
// Set the first sheet to be inactive.
// (Worksheet) osheets. get_item (1). Activate ();
Osheet = (worksheet) osheets. get_item (1 );
}
String strfilepathandname = httpcontext. Current. server. mappath (".") + "/EXCEL/" + sfile;
If (system. Io. file. exists (strfilepathandname ))
{
System. Io. file. Delete (strfilepathandname );
}
// Excel. workbooks. get_item (1). worksheets. Select (Excel. workbooks. get_item (1). worksheets [1]);
Osheet. saveas (strfilepathandname, Microsoft. office. interOP. excel. xlfileformat. xltemplate, type. missing, type. missing, type. missing, type. missing, Microsoft. office. interOP. excel. xlsaveasaccessmode. xlnochange, type. missing, type. missing, type. missing );
Obook. Close (false, type. Missing, type. Missing );
// Exit Excel and release the called com Resource
Excel. Quit ();
GC. Collect ();
// Excel. Save ("ddd.xls ");
Killprocess ("Excel ");
}

Public static void out2excel (string stablename, string URL)
{
Microsoft. Office. InterOP. Excel. Application oexcel = new Microsoft. Office. InterOP. Excel. Application ();
Workbooks obooks;
Workbook obook;
Sheets osheets;
Worksheet osheet;
Range ocells;
String sfile = "", stemplate = "";
//
System. Data. datatable dt = NULL; // tableout (stablename). Tables [0];
Sfile = URL + "myexcel.xls ";
Stemplate = URL + "mytemplate.xls ";
//
Oexcel. Visible = false;
Oexcel. displayalerts = false;
// Define a new workbook
Obooks = oexcel. workbooks;
Obooks. open (stemplate, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing );
Obook = obooks. get_item (1 );
Osheets = obook. worksheets;
Osheet = (worksheet) osheets. get_item (1 );
// Name the sheet
Osheet. Name = "sheet1 ";
// (Worksheet) osheets. Add (type. Missing, type. Missing );
Ocells = osheet. cells;
// Call the dumpdata process to import the data to excel
// Dumpdata (DT, ocells );
// Save
Osheet. saveas (sfile, Microsoft. office. interOP. excel. xlfileformat. xltemplate, type. missing, type. missing, type. missing, type. missing, Microsoft. office. interOP. excel. xlsaveasaccessmode. xlnochange, type. missing, type. missing, type. missing );
Obook. Close (false, type. Missing, type. Missing );
// Exit Excel and release the called com Resource
Oexcel. Quit ();
GC. Collect ();
Killprocess ("Excel ");
}
Private Static void killprocess (string processname)
{
System. Diagnostics. Process myproc = new system. Diagnostics. Process ();
// Obtain all opened Processes
Try
{
Foreach (process thisproc in process. getprocessesbyname (processname ))
{
If (! Thisproc. closemainwindow ())
{
Thisproc. Kill ();
}
}
}
Catch (exception exc)
{
Throw new exception ("", exc );
}
}
}

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.