2010-02-04 68 views
8

Tôi có dữ liệu đến từ bảng mô hình Dữ liệu thực thể trên trang ASP.NET của tôi. Bây giờ tôi phải xuất dữ liệu này sang Excel khi nhấp vào nút.Làm cách nào để xuất dữ liệu sang Excel bằng LINQ to Entity?

Nếu nó được sử dụng OLEDB, nó là thẳng về phía trước vì nó là ở đây: http://csharp.net-informations.com/excel/csharp-excel-oledb-insert.htm

Đây là chức năng của tôi để đọc dữ liệu từ yêu cầu bảng:

var model = from i in myEntity.Inquiries 
      where i.User_Id == 5 
         orderby i.TX_Id descending 
         select new { 
          RequestID = i.TX_Id, 
          CustomerName = i.CustomerMaster.FirstName, 
          RequestDate = i.RequestDate, 
          Email = i.CustomerMaster.MS_Id, 
          DocDescription = i.Document.Description, 
          ProductName = i.Product.Name 
+0

http://www.hanselman.com/blog/BackToBasicsKeepItSimpleAndDevelopYourSenseOfSmellFromLinqToCSV.aspx –

Trả lời

1

Bạn chỉ có thể viết một chuỗi đại diện của dữ liệu của bạn - tab được phân tách cho mỗi trường và \ r \ n được phân tách cho mỗi hàng. Sau đó, phát trực tuyến trình duyệt đó dưới dạng tệp .csv, tệp này sẽ tự động mở trong Excel.

+0

Tôi đã tìm thấy giải pháp. Đây rồi. http://stackoverflow.com/questions/1932568/exporting-a-html-table-to-excel – Rita

+0

Bạn cũng có thể xem tại đây: http://stackoverflow.com/questions/151005/create-excel-xls -and-xlsx-file-from-c / –

0

Bạn có thể xem xét sử dụng SpreadSheetML, về cơ bản đó là tệp XML có quy trình Cocoon được đề cập ở trên tệp xml để khởi chạy ứng dụng excel khi nhấp đúp vào tệp xml. Mẫu cho SpreadSheetML được cung cấp như dưới đây.

<?xml version="1.0"?> 
<?mso-application progid="Excel.Sheet"?> 
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:dt="urn:schemas-microsoft-com:datatypes" xmlns:ms="urn:schemas-microsoft-com:xslt"> 
    <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> 
     <Author xmlns="urn:schemas-microsoft-com:office:spreadsheet">Author<"/Author> 
     <LastAuthor xmlns="urn:schemas-microsoft-com:office:spreadsheet">Author<"/LastAuthor> 
     <Created xmlns="urn:schemas-microsoft-com:office:spreadsheet"/> 
     <LastSaved xmlns="urn:schemas-microsoft-com:office:spreadsheet"/> 
     <Company xmlns="urn:schemas-microsoft-com:office:spreadsheet">Author<"/Company> 
     <Version xmlns="urn:schemas-microsoft-com:office:spreadsheet">11.8132<"/Version> 
    </DocumentProperties> 
    <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> 
     <WindowHeight xmlns="urn:schemas-microsoft-com:office:spreadsheet">12660<"/WindowHeight> 
     <WindowWidth xmlns="urn:schemas-microsoft-com:office:spreadsheet">19020<"/WindowWidth> 
     <WindowTopX xmlns="urn:schemas-microsoft-com:office:spreadsheet">120<"/WindowTopX> 
     <WindowTopY xmlns="urn:schemas-microsoft-com:office:spreadsheet">105<"/WindowTopY> 
     <ProtectStructure xmlns="urn:schemas-microsoft-com:office:spreadsheet">False<"/ProtectStructure> 
     <ProtectWindows xmlns="urn:schemas-microsoft-com:office:spreadsheet">False<"/ProtectWindows> 
    </ExcelWorkbook> 
    <Styles> 
     <Style ss:ID="s21"> 
      <NumberFormat ss:Format="Percent"/> 
     </Style> 
     <Style ss:ID="s22"> 
      <NumberFormat ss:Format="[ENG][$-409]d\-mmm\-yyyy;@"/> 
     </Style> 
     <Style ss:ID="s23"> 
      <NumberFormat ss:Format="mm/dd/yyyy;@"/> 
     </Style> 
     <Style ss:ID="s24"> 
      <Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/> 
      <Font x:Family="Swiss" ss:Bold="1"/> 
     </Style> 
     <Style ss:ID="Default" ss:Name="Normal"> 
      <Alignment ss:Vertical="Bottom"/> 
      <Borders/> 
      <Font/> 
      <Interior/> 
      <NumberFormat/> 
      <Protection/> 
     </Style> 
    </Styles> 
    <Worksheet ss:Name="SomeSheetName"> 
     <Table ss:ExpandedColumnCount="33" ss:ExpandedRowCount="5768" x:FullColumns="1" x:FullRows="1"> 
      <Column ss:Width="111"/> 
       <Row> 
        <Cell ss:StyleID="s24"> 
         <Data ss:Type="String">ABCD<"/Data> 
        </Cell> 
        <Cell ss:StyleID="s24"> 
         <Data ss:Type="String">ABCD<"/Data> 
        </Cell> 
        <Cell ss:StyleID="s24"> 
         <Data ss:Type="String">ABCD<"/Data> 
        </Cell> 
        <Cell ss:StyleID="s24"> 
         <Data ss:Type="String">ABCD<"/Data> 
        </Cell> 
        <Cell ss:StyleID="s24"> 
         <Data ss:Type="String">ABCD<"/Data> 
        </Cell> 
       </Row> 
      </Column> 
     </Table> 
     <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> 
      <Selected/> 
      <ProtectObjects>False<"/ProtectObjects> 
      <ProtectScenarios>False<"/ProtectScenarios> 
     </WorksheetOptions> 
    </Worksheet> 
</Workbook> 

Hy vọng điều này sẽ hữu ích.

0

Như đã đề cập trước đây, cách dễ nhất để xuất dữ liệu sang excel là tạo bản trình bày văn bản hoặc xml. Đối với tôi, tôi thích sử dụng công cụ tạo mẫu văn bản SpreadSheetML và T4 để tạo tệp. Bạn có thể xem qua mẫu T4 ở đây: http://lilium.codeplex.com/SourceControl/changeset/view/40985#803959.

Nếu bạn quyết định sử dụng T4, hãy nhớ rằng T4 là một phần của MS Visual Studio và bạn KHÔNG THỂ phân phối riêng biệt. Vấn đề có thể được giải quyết bằng cách cài đặt Visual Studio Express Edition trên máy mục tiêu.

Hoặc bạn có thể sử dụng công cụ tạo khuôn mẫu aspx tích hợp, được sử dụng để tạo lớp xem aspx. Hãy xem nó được thực hiện ở đây như thế nào [oops, nó không cho phép tôi đăng thêm siêu liên kết, nếu bạn vẫn quan tâm, hãy cho tôi biết] (lưu ý rằng đây là ứng dụng thế giới thực nên mã khá lớn và bẩn). Công cụ Aspx xử lý thẻ Kiểu theo cách riêng của nó, vì vậy bạn sẽ phải sử dụng tên đủ điều kiện để làm cho nó hoạt động, định dạng tự động trong Visual Studio cũng sẽ không hoạt động chính xác.

3

Bạn vẫn có thể chèn vào bảng tính Excel bằng cách sử dụng cùng một kỹ thuật như được xác định trong bài viết được liên kết.

Chỉ cần sử dụng mã giả sau

try 
{ 
    System.Data.OleDb.OleDbConnection MyConnection ; 
    System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand(); 
    string sql = null; 
    MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\csharp.net-informations.xls';Extended Properties=Excel 8.0;"); 
    MyConnection.Open(); 
    myCommand.Connection = MyConnection; 

    myCommand.CommandText = "Insert into [Sheet1$] (id,name) values('@p1', '@p2')"; 
    myCommand.Parameters.Add("@p1", OleDbType.VarChar, 100); 
    myCommand.Parameters.Add("@p2", OleDbType.VarChar, 100); 

    // define query to entity data model 
    var model = from i in myEntity.Inquiries select i; 

    foreach(var m in model) 
    {  
     cmd.Parameters["@p1"].Value = m.RequestID; 
     cmd.Parameters["@p2"].Value = m.CustomerName; 
     // .. Add other parameters here 
     cmd.ExecuteNonQuery(); 
    } 
    } 
1

Bạn có thể sử dụng sự phản chiếu để lấy danh sách các thuộc tính, sau đó sử dụng danh sách thuộc tính đó (và phản chiếu) để kết quả truy vấn của bạn vào một DataTable ADO.Net cũ. DataTable có WriteXML, có thể được sử dụng để lưu trữ một tệp XML tạm thời trong thư mục temp (sử dụng System.IO). Sau đó, chỉ cần sử dụng OpenXML trong ứng dụng Excel.

Tôi có các mẫu mã tại http://social.msdn.microsoft.com/Forums/en-US/whatforum/thread/69869649-a238-4af9-8059-55499b50dd57 nếu bạn muốn thử phương pháp này.IMO này có vẻ là nhanh nhất (ít nhất, nhanh hơn rất nhiều khi cố gắng viết trực tiếp lên Excel), dễ nhất (ít nhất, dễ dàng hơn nhiều khi tự mình chuyển đổi truy vấn của bạn sang một số định dạng XML) và phương pháp tái sử dụng nhiều nhất (cộng với mẫu mã chúng tôi đang ràng buộc muộn, vì vậy bạn có thể phát triển chống lại môi trường hỗn hợp giả sử ít nhất là Excel 2003).

Các vấn đề liên quan