C# 很好。我尝试了您提供的代码,现在发生的是当excel文件被下载时,它显示已损坏,但仍然可以打开,当我打开它时,它包含表单的布局。当我打开文件副本时,它会显示数据,但会一直重复到最后一个单元格。我不知道发生了什么。在设置objData之前,能否在调试模式下检

C# 很好。我尝试了您提供的代码,现在发生的是当excel文件被下载时,它显示已损坏,但仍然可以打开,当我打开它时,它包含表单的布局。当我打开文件副本时,它会显示数据,但会一直重复到最后一个单元格。我不知道发生了什么。在设置objData之前,能否在调试模式下检,c#,asp.net,excel,C#,Asp.net,Excel,很好。我尝试了您提供的代码,现在发生的是当excel文件被下载时,它显示已损坏,但仍然可以打开,当我打开它时,它包含表单的布局。当我打开文件副本时,它会显示数据,但会一直重复到最后一个单元格。我不知道发生了什么。在设置objData之前,能否在调试模式下检查pDTSubconOneLineList是否不包含重复值?写入Excel看起来还可以。我怀疑您的数据选择可能有问题。以前用于导出数据的代码是通过RDLC编写的,这是由其他程序员完成的。”ReportDataSource pRds=新的Repo


很好。我尝试了您提供的代码,现在发生的是当excel文件被下载时,它显示已损坏,但仍然可以打开,当我打开它时,它包含表单的布局。当我打开文件副本时,它会显示数据,但会一直重复到最后一个单元格。我不知道发生了什么。在设置objData之前,能否在调试模式下检查pDTSubconOneLineList是否不包含重复值?写入Excel看起来还可以。我怀疑您的数据选择可能有问题。以前用于导出数据的代码是通过RDLC编写的,这是由其他程序员完成的。”ReportDataSource pRds=新的ReportDataSource(“USP_RPT_Subco_ONE_LINE_LIST”,pDTSubconOneLineList);'这是数据源。然后在当前的示例中,我只得到了datatable,我认为这就是为什么它在导出时会重复。如何包含与旧代码相同的sp?
try
        {

            UpdateDataSelection();

            string pFileName = "SubconOneLineList_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";

            string pSubConCodeLists = string.Empty;
            string pJobCodeKey = string.Empty;

            string pCountryCode = string.Empty;
            string pWorkCategoryCode = string.Empty;
            string pCapacityCode = string.Empty;
            string pOperativeCountryCode = string.Empty;

            string pNameAbbr = string.Empty;
            string pAffiliates = string.Empty;

            pSubConCodeLists = string.Join(",", DataSelection);
            if (pSubConCodeLists == string.Empty)
            {
                GetSubconDatabaseFilter(out pSubConCodeLists, out pNameAbbr, out pJobCodeKey, out pCountryCode, out pOperativeCountryCode, out pWorkCategoryCode, out pAffiliates, out pCapacityCode);
            }

            string pGridFilter = (rgvSubcontractor.MasterTableView.FilterExpression == null ? string.Empty : rgvSubcontractor.MasterTableView.FilterExpression);
            string pSortString = "";
            if (rgvSubcontractor.MasterTableView.SortExpressions != null)
            {
                pSortString = ((rgvSubcontractor.MasterTableView.SortExpressions.GetSortString() == null) || (rgvSubcontractor.MasterTableView.SortExpressions.GetSortString() == string.Empty) ? pSortString : rgvSubcontractor.MasterTableView.SortExpressions.GetSortString());
            }
            pSortString = (pSortString == string.Empty ? "COMPANY_NAME ASC" : pSortString + ", COMPANY_NAME ASC");

            DataTable pDTSubconOneLineList = mSubContractorBS.getRptSubContractorOneLineList(pSubConCodeLists, pNameAbbr, string.Empty, string.Empty, pJobCodeKey, pCountryCode, pOperativeCountryCode, pWorkCategoryCode, pAffiliates, pCapacityCode);
            DataView pDVSubconOneLineList = new DataView(pDTSubconOneLineList);
            if (pGridFilter != string.Empty)
            {
                pDVSubconOneLineList.RowFilter = pGridFilter;
            }
            pDVSubconOneLineList.Sort = pSortString;
            pDTSubconOneLineList = pDVSubconOneLineList.ToTable();

            pDTSubconOneLineList.TableName = "USP_RPT_SUBCON_ONE_LINE_LIST";


            Process[] processList = Process.GetProcesses();


            string path = Server.MapPath("~") + "\\SIS\\Template\\Download\\Subcon_Profile_List_Import_Template.xlsx";
            //string targetPath = Convert.ToString(Session["App_Data_Path"]) + "EXPORT_OUTPUT";
            string targetPath = Convert.ToString(Server.MapPath("~")) + "EXPORT_OUTPUT";
            string destFile = System.IO.Path.Combine(targetPath, pFileName);

            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }

            File.Copy(path, destFile, true);

            object misValue = System.Reflection.Missing.Value;

            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(destFile, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[1];
            xlWorkSheet.get_Range("A2", "AN" + xlWorkSheet.Rows.Count.ToString()).Clear();

            object[,] objData = null;

            int rowcount = pDTSubconOneLineList.Rows.Count;

            objData = new Object[pDTSubconOneLineList.Rows.Count, pDTSubconOneLineList.Columns.Count];

            for (int row = 0; row < pDTSubconOneLineList.Rows.Count; row++)
            {
                for(int column= 0; column < pDTSubconOneLineList.Columns.Count; column++)
                {
                    objData[row, column] = pDTSubconOneLineList.Rows[row][column].ToString();
                }
            }

            ((Excel.Worksheet)xlWorkBook.Sheets[1]).Select(Type.Missing);

            xlWorkBook.Save();
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
            xlWorkSheet = null;
            xlWorkBook = null;
            xlApp = null;
            GC.Collect();

            string pMimeType = string.Empty;
            string pEncoding = string.Empty;
            string pExtension = string.Empty;

            Response.Buffer = true;
            Response.Clear();
            Response.AppendCookie(new HttpCookie("fileDownloadToken", hdDownLoadToken.Value));
            Response.ContentType = pMimeType;
            Response.AddHeader("content-disposition", "attachment; filename=" + pFileName);
            Response.Flush();
        }
        catch (Exception ex)
        {
            ErrorHelper.HandleError(ex);
        }
Excel.Range targetRange = xlWorkSheet.get_Range("A2", "AN" + xlWorkSheet.Rows.Count.ToString());
targetRange.Value = objData;