All pastes #624440 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-07-18 08:42 UTC
rendered paste body
Dans le controller/show :
    respond_to do |format|
      format.html # show.rhtml
      format.xml  { render :xml => @purchase_order.to_xml }
      format.js {
        if params[:line_items_page] or params[:line_items_sort]
          render :partial => "line_items_table"
       elsif params[:packages_page] or params[:packages_sort]
          render :partial => "packages_table"
       end
      }
      # This exports a purchase order to Excel file
      #format.xls { render :xml => @purchase_order.to_xls}
      format.xls {
        headers['Content-Type'] = "application/vnd.ms-excel"
        headers['Content-Disposition'] = 'attachment; filename="purchase-orders-export.xls"'
        headers['Cache-Control'] = ''
        render :file => export_excel.rxml, :layout => false
      }

    end


dans export_excel.rxml :

xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
xml.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:html' => "http://www.w3.org/TR/REC-html40",
  'xmlns:ss'   => "urn:schemas-microsoft-com:office:spreadsheet"
  }) do

  xml.Styles do
   xml.Style 'ss:ID' => 'Default', 'ss:Name' => 'Normal' do
     xml.Alignment 'ss:Vertical' => 'Bottom'
     xml.Borders
     xml.Font 'ss:FontName' => 'Verdana'
     xml.Interior
     xml.NumberFormat
     xml.Protection
   end
   xml.Style 'ss:ID' => 's22' do
     xml.NumberFormat 'ss:Format' => 'General Date'
   end
  end

  xml.Worksheet 'ss:Name' => 'Purchase Orders export' do
    xml.Table do

      # Header
      xml.Row do
        for column in PurchaseOrder.content_columns do
          xml.Cell do
            xml.Data column.human_name, 'ss:Type' => 'String'
          end
        end
      end

      # Rows
      for purchase_order in @purchase_orders
        xml.Row do
          for column in PurchaseOrder.content_columns do
            xml.Cell do
              xml.Data purchase_order.send(column.name), 'ss:Type' => 'String'
            end
          end
        end
      end

    end
  end

end