Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Export to Excel

Controller
class PersonController < ApplicationController
  def export
    headers['Content-Type'] = "application/vnd.ms-excel"
    headers['Content-Disposition'] = 'attachment; filename="report.xls"'
    headers['Cache-Control'] = ''
    @person = People.find(:all)
  end
end


View (link to export)
<%= link_to "Export as Excel", export_person_url %>


View (Export)
#Render partial so your Layout wont be sent to the spreadsheet
<%= render :partial => 'report' %>


Partial
<table border="1">
  <tr>
    <th>Name</th>
  </tr>
  <% @person.each do |p| %>
    <tr>
      <td><%= p.name %></td>
    <% end %>
  </tr>
</table>

Export french characters

// description of your code here

java -Dfile.encoding=CP850 YourClassName
export JAVA_OPTS=-Dfile.encoding=UTF-8

Levy Export

SELECT
lev_code id,
pro_catalogue catalogue,
pro_desc description,
pri_vat tax_id,
pri_trade2 trade_value,
COALESCE(NULLIF(pri_per, 0), 1) trade_per,
CASE WHEN lev_disabled = 'Y' THEN 1 WHEN pri_to < NOW() THEN 1 ELSE 0 END is_disabled ,
CASE WHEN lev_modified > pro_modified AND lev_modified > pri_modified THEN lev_modified WHEN pro_modified > lev_modified AND pro_modified > pri_modified THEN pro_modified ELSE pri_modified END updated_at,
pro_code product_id,
pro_stockcode stock_code,
pro_supplier supplier_id
FROM
levy
INNER JOIN product ON lev_product = pro_code
INNER JOIN price ON pri_product = pro_code
WHERE
pri_from <= NOW()
AND NOT EXISTS(SELECT * FROM price p WHERE p.pri_seq <> price.pri_seq AND p.pri_product = price.pri_product AND p.pri_from > price.pri_from)
AND pri_trade2 > 0;
OUTPUT TO 'd:\\levies.csv'

Forcing an SVN export to overwrite a directory

You can run --force to make an SVN export overwrite a folder rather than write into it

cd /usr/local/etc/
svn export --force --username user@textdrive.com --password apassword https://svn.textdrive.com/repos/setup/usr/local/etc/apache2