从ASP.NET生成Excel工作表

时间:2017-11-16 11:35:44

标签: c# asp.net excel

我刚开始研究asp.net。我想从数据库生成excel但它不起作用。以下是功能。 我想要的是在使用表单创建对象后,将填充的数据导出到Excel表格中 我的功能

    private string GeneratemyobjectExcelForm(Myobject myobject)
    {
        Process[] processesBefore = Process.GetProcessesByName("excel");

        string ret = "";
        object oMissing = System.Reflection.Missing.Value;
        object missing = System.Reflection.Missing.Value;
        string tempFilePath = Server.MapPath("~/Views") + "/myobject_Views.xlsx";
        string filename = Server.MapPath("~/myobjectfolder") + "/" + myobject.myobjectNumber.Replace('/', '-') + ".xlsx";
        File.Copy(tempFilePath, filename,true);
        Microsoft.Office.Interop.Excel.Application objExcel = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel.Workbook objworkbook;
        Microsoft.Office.Interop.Excel.Worksheet objworksheet;

        try
        {

            objExcel.DisplayAlerts = false;
            objExcel.Visible = false;

            objworkbook = objExcel.Workbooks.Open(filename, true, false,
                                                  Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                                  Type.Missing, Type.Missing, true, Type.Missing,
                                                  Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            // Get Excel processes after opening the file.
            Process[] processesAfter = Process.GetProcessesByName("excel");

            // Now find the process id that was created, and store it.
            int processID = 0;
            foreach (Process process in processesAfter)
            {
                if (!processesBefore.Select(p => p.Id).Conmyobjectins(process.Id))
                {
                    processID = process.Id;
                }
            }

            objworksheet = (Microsoft.Office.Interop.Excel.Worksheet)objworkbook.Sheets.get_Item("Appendix A");


            Microsoft.Office.Interop.Excel.Range selRange = objworksheet.get_Range("K3", "K3");
            selRange.set_Value(XlRangeValueDamyobjectType.xlRangeValueDefault, myobject.myobjectNumber);


            selRange = objworksheet.get_Range("D6", "D6");
            selRange.set_Value(XlRangeValueDamyobjectType.xlRangeValueDefault, myobject.name1);


            selRange = objworksheet.get_Range("D7", "D7");
            selRange.set_Value(XlRangeValueDamyobjectType.xlRangeValueDefault, myobject.name2);


            selRange = objworksheet.get_Range("D9", "D9");
            selRange.set_Value(XlRangeValueDamyobjectType.xlRangeValueDefault, myobject.name3);

            selRange = objworksheet.get_Range("D10", "D10");
            selRange.set_Value(XlRangeValueDamyobjectType.xlRangeValueDefault, myobject.name4);

            selRange = objworksheet.get_Range("D11", "D11");
            selRange.set_Value(XlRangeValueDamyobjectType.xlRangeValueDefault, string.Format("{0} - {1}",myobject.DateFromString,myobject.DateToString));

            selRange = objworksheet.get_Range("E14", "E14");
            selRange.set_Value(XlRangeValueDamyobjectType.xlRangeValueDefault, myobject.name5);
            selRange = objworksheet.get_Range("G14", "G14");
            selRange.set_Value(XlRangeValueDamyobjectType.xlRangeValueDefault, myobject.name6);

            selRange = objworksheet.get_Range("E15", "E15");
            selRange.set_Value(XlRangeValueDamyobjectType.xlRangeValueDefault, myobject.name7);
            selRange = objworksheet.get_Range("G15", "G15");
            selRange.set_Value(XlRangeValueDamyobjectType.xlRangeValueDefault, myobject.name8);

            selRange = objworksheet.get_Range("E16", "E16");
            selRange.set_Value(XlRangeValueDamyobjectType.xlRangeValueDefault, myobject.name9);
            selRange = objworksheet.get_Range("G16", "G16");
            selRange.set_Value(XlRangeValueDamyobjectType.xlRangeValueDefault, myobject.name10);

            objworkbook.Save();
            objworkbook.Close(Microsoft.Office.Interop.Excel.XlSaveAction.xlSaveChanges, Type.Missing, Type.Missing);
            objExcel.Quit();
            ret = filename.ToString();
            // And now kill the process.
            if (processID != 0)
            {
                Process process = Process.GetProcessById(processID);
                process.Kill();
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            objExcel = null;
            objworkbook = null;
            objworksheet = null;
            ReleaseComObject(objExcel);
            ReleaseComObject(objworkbook);
            ReleaseComObject(objworksheet);
        }

        return ret;
    }

0 个答案:

没有答案