2010-08-30 5 views
0

J'ai la date. Je dois importer ces valeurs DataTable Excel Template.How pour atteindre cetComment importer des données dans la feuille Excel

+0

toujours se souvenir, google est votre ami: http://www.codeproject.com/KB/office/ExcelDataTable.aspx http://msmvps.com/blogs/deborahk/archive/2009/07/ 23/writing-data-from-a-datable-to-excel.aspx http://www.dotnetspider.com/resources/29183-Export-DataTable-Excel-using-c-with.aspx – MaQleod

Répondre

1

Il y a un certain nombre d'options

  1. écrire les données sous forme de fichier CSV
  2. écrire les données comme un tableau HTML
  3. Utilisez Automation pour manipuler une instance Excel en cours d'exécution
  4. Utilisez OleDB Driver pour créer un fichier Excel. Another link from Microsoft.
  5. Utilisez une bibliothèque comme NPOI pour écrire un fichier Excel
  6. Utilisez une bibliothèque comme ExcelPackage pour écrire un fichier Excel
  7. Utilisez Office Open XML

Parmi les options, j'aime l'option 5 pour la performance et la simplicité, surtout lorsque cela est nécessaire côté serveur, l'option 6 est bonne si vous avez besoin de fichiers XLSX plutôt que XLS, l'option 7 a une courbe d'apprentissage raide par rapport à 5 et 6.

0

Essayez celui-ci -

// TO USE: 
      // 1) include COM reference to Microsoft Excel Object library 
      // add namespace... 
      // 2) using Excel = Microsoft.Office.Interop.Excel; 
      private static void Excel_FromDataTable(DataTable dt) 
      { 
       // Create an Excel object and add workbook... 
       Excel.ApplicationClass excel = new Excel.ApplicationClass(); 
       // true for object template??? 
       Excel.Workbook workbook = excel.Application.Workbooks.Add(true); 


       // Add column headings... 
       int iCol = 0; 
       foreach (DataColumn c in dt.Columns) 
       { 
        iCol++; 
        excel.Cells[1, iCol] = c.ColumnName; 
       } 
       // for each row of data... 
       int iRow = 0; 
       foreach (DataRow r in dt.Rows) 
       { 
        iRow++; 

        // add each row's cell data... 
        iCol = 0; 
        foreach (DataColumn c in dt.Columns) 
        { 
         iCol++; 
         excel.Cells[iRow + 1, iCol] = r[c.ColumnName]; 
        } 
       } 

       // Global missing reference for objects we are not defining... 
       object missing = System.Reflection.Missing.Value; 

       // If wanting to Save the workbook... 
       workbook.SaveAs("MyExcelWorkBook.xls", 
        Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing, 
        false, false, Excel.XlSaveAsAccessMode.xlNoChange, 
        missing, missing, missing, missing, missing); 

       // If wanting to make Excel visible and activate the worksheet... 
       excel.Visible = true; 
       Excel.Worksheet worksheet = (Excel.Worksheet)excel.ActiveSheet; 
       ((Excel._Worksheet)worksheet).Activate(); 

       // If wanting excel to shutdown... 
       ((Excel._Application)excel).Quit(); 
      } 
Questions connexes