public static string CetExcel(string ZYBJDM, string Type)
    {

        string path = System.AppDomain.CurrentDomain.BaseDirectory + "。。。\\。。。";
        string Mod = path + "\\ 、、、\\、、、";      
        ExamRegistrationBLL examBLL = new ExamRegistrationBLL();
        System.Data.DataTable dt = examBLL.GetExamInfo(参数1,参数2,、、、).Tables[0];
        Excel.Application App = new Excel.Application();
        if (App == null)
            return string.Empty;
        App.Visible = false;
        App.UserControl = true;

        Excel.Workbooks workbooks = App.Workbooks;
        Excel._Workbook workbook = workbooks.Open(Mod, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
        Excel.Sheets sheets = workbook.Sheets;
        Excel._Worksheet worksheet = (Excel._Worksheet)sheets.get_Item(1);
        if (worksheet == null)
            return string.Empty;

        worksheet.Cells[1, 1] = Type + "setName";
        int StartIndex = 4;
        int Num = 1;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            worksheet.Cells[StartIndex, 1] = Num.ToString();
            worksheet.Cells[StartIndex, 2] = dt.Rows[i][" "].ToString();
            worksheet.Cells[StartIndex, 3] = dt.Rows[i][" "].ToString();
            worksheet.Cells[StartIndex, 4] = dt.Rows[i][" "].ToString().Trim();
            worksheet.Cells[StartIndex, 5] = dt.Rows[i][" "].ToString();
            worksheet.Cells[StartIndex, 6] = dt.Rows[i][" "].ToString();
            worksheet.Cells[StartIndex, 7] = dt.Rows[i][" "].ToString();
            worksheet.Cells[StartIndex, 8] = SwitchXZ(dt.Rows[i][" "].ToString());
            Num++;
            StartIndex++;
        }
        worksheet.get_Range(worksheet.Cells[3, 1], worksheet.Cells[StartIndex - 1, 9]).Borders.Value = 1;

        string SaveUrl = path + "\\ \\" + name.Substring(2, 7) + ".xls";
        try
        {
            if (File.Exists(SaveUrl))
            {
                File.Delete(SaveUrl);
            }
            workbook.SaveAs(SaveUrl, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            workbook.Close(false, Missing.Value, Missing.Value);
            App.Workbooks.Close();
            App.Quit();

            System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(sheets);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(App);

            GC.Collect();
        }
        catch (Exception ex) { throw ex; }
        return name.Substring(2, 7);
    }

posted on 2013-04-08 21:53  叶城宇  阅读(194)  评论(0)    收藏  举报