EXCEL 列宽

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();//引用Excel对象
        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);//引用Excel工作簿
        excel.Visible = false;//使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;
            //命名该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);
            //设定第一个sheet未活动sheet
            //((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);
        //退出Excel,并且释放调用的COM资源
        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;
        //定义一个新的工作簿
        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);
        //命名该sheet
        oSheet.Name = "Sheet1";
        //(Worksheet)oSheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        oCells = oSheet.Cells;
        //调用dumpdata过程,将数据导入到Excel中去
        //DumpData(dt, oCells);
        //保存
        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);
        //退出Excel,并且释放调用的COM资源
        oExcel.Quit();
        GC.Collect();
        KillProcess("Excel");
    }
    private static void KillProcess(string processName)
    {
        System.Diagnostics.Process myproc = new System.Diagnostics.Process();
        //得到所有打开的进程
        try
        {
            foreach (Process thisproc in Process.GetProcessesByName(processName))
            {
                if (!thisproc.CloseMainWindow())
                {
                    thisproc.Kill();
                }
            }
        }
        catch (Exception Exc)
        {
            throw new Exception("", Exc);
        }
    }
}

你可能感兴趣的:(Excel)