创建一个只有excel而不是多个

时间:2016-03-24 09:39:17

标签: xml excel linq file oledbconnection

处理excel文件并添加从xml电子表格中获取的行后,在创建后保存每个文件。问题是我必须将所有内容保存在一个文件中。你对此有什么解决方案吗?非常感谢。

[代码]  公共类RecordExcel         {

        public string NumeroContratto { set; get; }
        public string CodiceCliente { set; get; }
        public string ImportoTassa { set; get; }
        public string CostoEsazione { set; get; }
        public string CostoContratto { set; get; }
        public string Spese { set; get; }
        public string Targa { set; get; }
        public string Servizi { set; get; }
        public string Sconto { set; get; }
        public string Commissioni { set; get; }
        public string Totale { set; get; }

    }

private void AvviaGenerazioneReports()         {

        DataTable TabellaDati = GetTable();

        foreach (DataRow rigo in TabellaDati.Rows)
        {
            string PROV = rigo["AC"].ToString();
            string NomeFileExcel = rigo["Stampa"].ToString();
            string NomeFileXML = rigo["NomeRIDProdotto"].ToString();
            string NomeFoglioDati = NomeFileExcel.Replace(".xlsx", "").Replace(".xls", "");

            GeneraExcel(PROV, NomeFileExcel, NomeFileXML, NomeFoglioDati);

        }
    }

    private void GeneraExcel(string Provincia, string NomeFileExcel, string NomeFileXML, string NomeFoglioDati)
    {
        // ExportToExcel(GridView1, "ExportedUserDetail");
        string prov = Provincia;
        string PERCORSO = Classi.Costanti.PercorsoCartellaReport;
       // string PERCORSOxml = Classi.Costanti.PercorsoCartellaXML;
        string filename = PERCORSO + "/" + prov + "/" + NomeFileExcel;
        ViewState["file"] = NomeFileExcel;
        //Leggo id dall'Excel
        var ElencoRecords = getDatiExcel(filename);
        //Genero il nuovo file Excel
        SalvaDatiNuovoExcel (ElencoRecords, PERCORSO, prov, NomeFileXML, filename);

    }
    private void SalvaDatiNuovoExcel(List <RecordExcel> ElencoRecords,string PERCORSO, string  prov,string  NomeFileXML, string filename)
    {
        string PercorsoXML = Classi.Costanti.PercorsoCartellaXML+"/"+prov+"/"+NomeFileXML;

        Microsoft.Office.Interop.Excel._Application oApp;
        Microsoft.Office.Interop.Excel._Worksheet oSheet;
        Microsoft.Office.Interop.Excel._Workbook oBook;

        oApp = new Microsoft.Office.Interop.Excel.Application();
        oBook = oApp.Workbooks.Add(true);
        oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oBook.Worksheets.get_Item(1)

            oSheet.Cells[1, 1] = "Contratto numero";
            oSheet.Cells[1, 2] = "Codice Cliente / RIF  Bancario";
            oSheet.Cells[1, 3] = "Targa";
            oSheet.Cells[1, 4] = "Tassa";
            oSheet.Cells[1, 5] = "Costo contratto";
            oSheet.Cells[1, 6] = "Spese";
            oSheet.Cells[1, 7] = "Commisioni";
            oSheet.Cells[1, 8] = "Servizi";
            oSheet.Cells[1, 9] = "Sconti";
            oSheet.Cells[1, 10] = "Totale";
            oSheet.Cells[1, 11] = "RID effettuato";

            int Indice = 2;
            foreach (RecordExcel record in ElencoRecords)
            {

                oSheet.Cells[Indice, 1] = record.NumeroContratto;
                oSheet.Cells[Indice, 2] = record.CodiceCliente;
                oSheet.Cells[Indice, 3] = record.Targa;
                oSheet.Cells[Indice, 4] = record.ImportoTassa;
                oSheet.Cells[Indice, 5] = record.CostoContratto;
                oSheet.Cells[Indice, 6] = record.Spese;
                oSheet.Cells[Indice, 7] = record.Commissioni;
                oSheet.Cells[Indice, 8] = record.Servizi;
                oSheet.Cells[Indice, 9] = record.Sconto;
                oSheet.Cells[Indice, 10] = record.Totale;

                oSheet.Cells[Indice, 11] = ImportoXML(PercorsoXML, record.CodiceCliente);
                Indice++;
            }
        }


         if (oApp.Application.Sheets.Count < 1)
        {

            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oBook.Worksheets.Add("", "", "", "");
        }
        else
        {
            oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oApp.Worksheets[1];
        }

        string PercorsoFileElaborato = string.Format ("{0}_{1}.xls", filename.Replace (".xlsx", "").Replace (".xls", ""),Guid.NewGuid());
        oBook.SaveAs(PercorsoFileElaborato, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing,
            false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        oBook.Close(true, null, null);
        oApp.Quit();
        //////
    }[/code]

0 个答案:

没有答案