2012-12-18 14 views
12

Sunucumda yeni yayınlanmış bir Excel elektronik tablosunu nasıl okuyabilirim? Bir şey aradım ancak dosyam yolu olmayan bir Excel elektronik tablosunu nasıl okuyacağımı buldum. Office Interops meclisleri ihtiyaçBellekte bir Excel elektronik tablosu okuyun

public ActionResult Import(HttpPostedFileBase file) 
{ 
    var excel = new ExcelQueryFactory(file); //using linq to excel 
} 
+1

Olası yinelenen: kod dosyasının en üstünde

ithal ad [Okuma bir akıştan excel dosyası] (http: // stackoverflow .com/sorular/560435/okuma-excel dosyası-dan-bir-stream)? –

+0

Aşağıdaki gibi problemleri göz önünde bulundurmanız gerekir: Tablo ilk sayfada değilse ne olur? Ya belirttiğiniz aralıkta değilse? Excel, OleDb yeteneklerine sahiptir, ancak sekmeleri almak için GetOleDbSchema() gibi yöntemleri kullanmanız ve sonra sorgulanacak aralığı bilmeniz gerekir. Bu mümkündür, ancak sadece 3 satır kod değil. –

+0

@DominicZukiewicz Bu benim durumumda gerçek bir sorun değil çünkü her yükleme dosyası aynı şemaya sahip olacak. – MuriloKunze

cevap

13

Maalesef LinqToExcel ile bir akıştan bir elektronik tablo okumak mümkün değildir.

Bunun nedeni, elektronik tablolardan okumak için OLEDB kullanması ve bir akıştan okunamamasıdır.

19

Aynı sorunla karşılaşıyordum, ancak ücretli bir hizmete geçmek istemedim, bu yüzden yaptığım şey buydu. İşte

public class DataImportHelper : IDisposable 
{ 
    private readonly string _fileName; 
    private readonly string _tempFilePath; 

    public DataImportHelper(HttpPostedFileBase file, string tempFilePath) 
    { 
     _fileName = file.FileName; 
     _tempFilePath = Path.Combine(tempFilePath, _fileName); 
     (new FileInfo(_tempFilePath)).Directory.Create(); 
     file.SaveAs(_tempFilePath); 
    } 

    public IQueryable<T> All<T>(string sheetName = "") 
    { 
     if (string.IsNullOrEmpty(sheetName)) 
     { 
      sheetName = (typeof (T)).Name; 
     } 

     var excelSheet = new ExcelQueryFactory(_tempFilePath); 

     return from t in excelSheet.Worksheet<T>(sheetName) 
       select t; 
    } 

    public void Dispose() 
    { 
     File.Delete(_tempFilePath); 
    } 

} 

bir Testi İşte

[Fact] 
    public void AcceptsAMemoryStream() 
    { 
     MemoryFile file; 

     using (var f = File.OpenRead("SampleData.xlsx")) 
     { 
      file = new MemoryFile(f, "multipart/form-data", "SampleData.xlsx"); 

      using (var importer = new DataImportHelper(file, "Temp/")) 
      { 
       var products = importer.All<Product>(); 

       Assert.NotEmpty(products); 

      } 
     } 
    } 

MemoryFile.cs olmasıdır. Bu dosya sadece test için kullanılır. Bu sadece HttpPostedFileBase'in bir uygulamasıdır, böylece kontrol cihazlarınızı ve küçük yardımcımı test edebilirsiniz. Bu başka bir yazıdan ödünç alındı.

public class MemoryFile : HttpPostedFileBase 
    { 
     Stream stream; 
     string contentType; 
     string fileName; 

     public MemoryFile(Stream stream, string contentType, string fileName) 
     { 
      this.stream = stream; 
      this.contentType = contentType; 
      this.fileName = fileName; 
     } 

     public override int ContentLength 
     { 
      get { return (int)stream.Length; } 
     } 

     public override string ContentType 
     { 
      get { return contentType; } 
     } 

     public override string FileName 
     { 
      get { return fileName; } 
     } 

     public override Stream InputStream 
     { 
      get { return stream; } 
     } 

     public override void SaveAs(string filename) 
     { 
      using (var file = File.Open(filename, FileMode.Create)) 
       stream.CopyTo(file); 
     } 
    } 
+0

Nice sorusunu yanıtlamıyor, Sorunumu çözmek için böyle bir şey yaptım. – MuriloKunze

+0

Teşekkürler Brett Allred, bu yardımcı oldu! – escist

0

Sen bellekte Excel hesap okumak için HttpPostedFileBase arasında InputStream özelliğini kullanabilirsiniz.

Sizin durumunuzda bulunan akıştan excel içeriğini okumak için ClosedXML nuget paketini kullanıyorum. Excel dosyası (aka çalışma kitabı) için akış işaret eden akışı alır basit bir aşırı yüklenme vardır.

using ClosedXML.Excel; 

Kaynak kodu: Bu sorunun

public ActionResult Import(HttpPostedFileBase file) 
{ 
    //HttpPostedFileBase directly is of no use so commented your code 
    //var excel = new ExcelQueryFactory(file); //using linq to excel 
    var stream = file.InputStream; 
    if (stream.Length != 0) 
    { 
     //handle the stream here 
     using (XLWorkbook excelWorkbook = new XLWorkbook(stream)) 
     { 
      var name = excelWorkbook.Worksheet(1).Name; 
      //do more things whatever you like as you now have a handle to the entire workbook. 
      var firstRow = excelWorkbook.Worksheet(1).Row(1); 
     } 
    } 
} 
İlgili konular