2013-03-22 49 views
0

我有一個包含URL集合的Excel工作表,我正在從Excel中逐一讀取這些URL並將URL傳遞給另一個服務以檢索某些數據。 As in this link 現在,我有這樣的如何創建Excel工作表並從列表中插入數據?

internal class Record 
    { 
     public string Id { get; set; } 
     public string url { get; set; } 
     public string Name { get; set; } 
     public string Age { get; set; } 
    } 

類現在,我創建的類List<Record> rec= new List<Record>(); 的列表,並從服務中我得到了一些100的記錄,這是在列表中REC。 我想達到什麼是創建另一個Excel工作表與列名編號,地址,姓名,年齡 和行REC將數據傳輸到Excel工作表中的行。(這是什麼在做反向以上鍊接)excel應該看起來像,

Id    Url                     Name Age 
1  http://www.sample.com/term=100898731%5Buid%5D&cmd=DetailsSearch&report=xml&format=text  Tom  10 
2  http://www.sample.com/term==101120693%5Buid%5D&cmd=DetailsSearch&report=xml&format=text  Jerry 11 
3  http://www.sample.com/term==100893225%5Buid%5D&cmd=DetailsSearch&report=xml&format=text  Jose 10 

如何實現相同?

回答

1

你曾經使用Open XML SDK

using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Create(
    System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SPREADSHEET_NAME), 
    SpreadsheetDocumentType.Workbook)) 
{ 
    // create the workbook 
    spreadSheet.AddWorkbookPart(); 
    spreadSheet.WorkbookPart.Workbook = new Workbook();  // create the worksheet 
    spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>(); 
    spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet = new Worksheet(); 

    // create sheet data 
    spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.AppendChild(new SheetData()); 

    // create row 
    spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.First().AppendChild(new Row()); 

    // create cell with data 
    spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.First().First().AppendChild(   
      new Cell() { CellValue = new CellValue("101") }); 

    // save worksheet 
    spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.Save(); 

    // create the worksheet to workbook relation 
    spreadSheet.WorkbookPart.Workbook.AppendChild(new Sheets()); 
    spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>().AppendChild(new Sheet() 
     { 
      Id = spreadSheet.WorkbookPart.GetIdOfPart(spreadSheet.WorkbookPart.WorksheetParts.First()), 
      SheetId = 1, 
      Name = "test" 
     }); 

    spreadSheet.WorkbookPart.Workbook.Save(); 
} 

循環在你的名單,並追加更多的細胞考慮。

相關問題