2013-06-10 5 views
0

J'ai joint une photo ci-dessous pour mieux comprendre le format que je veux pour mon Excel. Je n'ai aucune idée comment commencer à formater Excel en utilisant ASP.NET du tout. Merci de me guider. Merci d'avance.Format et exportation ASP.NET Excel

enter image description here

Répondre

0

Essayez de mettre au-dessus de format dans Datatable de jeu de données & puis juste passer que datatable à la fonction suivante.

string path = Context.Server.MapPath("~/ExcelData/test.xslx"); 
ExportDataSet(ds, path); 

ExportDataSet private void (DataSet ds, destination string) { en utilisant (classeur var = SpreadsheetDocument.Create (destination, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook)) { var = workbookPart workbook.AddWorkbookPart ()

workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook(); 

    workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets(); 

    foreach (System.Data.DataTable table in ds.Tables) 
    { 

     var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>(); 
     var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData(); 
     sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData); 

     DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>(); 
     string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart); 

     uint sheetId = 1; 
     if (sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Count() > 0) 
     { 
      sheetId = 
       sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Select(s => s.SheetId.Value).Max() + 1; 
     } 

     DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName }; 
     sheets.Append(sheet); 

     DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row(); 

     List<String> columns = new List<string>(); 
     foreach (System.Data.DataColumn column in table.Columns) 
     { 
      columns.Add(column.ColumnName); 

      DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell(); 
      cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String; 
      cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName); 
      headerRow.AppendChild(cell); 
     } 


     sheetData.AppendChild(headerRow); 

     foreach (System.Data.DataRow dsrow in table.Rows) 
     { 
      DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row(); 
      foreach (String col in columns) 
      { 
       DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell(); 
       cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String; 
       cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); // 
       newRow.AppendChild(cell); 
      } 

      sheetData.AppendChild(newRow); 
     } 

    } 
} 

}

using DocumentFormat.OpenXml.Packaging; 
using DocumentFormat.OpenXml.Spreadsheet; 
using DocumentFormat.OpenXml; 
+0

Désolé code collé est pas en bonne forme, de sorte que vous pouvez le voir sur ce lien. http://stackoverflow.com/questions/15826614/how-to-write-data-on-new-sheet-of-excel –

+0

Si je dis que je veux que ce soit sur un bouton. Quels sont le code exact que je devrais écrire? L'esprit me fournissant le code complet? Merci beaucoup! –

+0

Lorsque vous cliquez sur un bouton, vous devez importer des données dans le jeu de données par procédure stockée, afin que vous puissiez passer à la fonction écrite ci-dessus. Il fera tout et créera un fichier excel au chemin spécifié. Téléchargez openXML Sdk (2.0) et ajoutez des références au projet et à la page. –

Questions connexes