1

我有一个包含 URL 集合的 excel 表,我正在从 excel 中逐个读取这些 URL,并将 URL 传递给另一个服务以检索一些数据。如在此链接中 现在,我有这样的课程

 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中。我想要实现的是创建另一个列名为Id,Url,Name ,Age的 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 

如何实现相同的?

4

1 回答 1

2

您是否考虑过使用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();
}

循环您的列表并附加更多单元格。

于 2013-03-22T07:06:12.297 回答