Skip to content

Instantly share code, notes, and snippets.

@ninnemana
Created September 5, 2012 22:24
Show Gist options
  • Save ninnemana/3646325 to your computer and use it in GitHub Desktop.
Save ninnemana/3646325 to your computer and use it in GitHub Desktop.
CSV Export
prices, _ := pricing.Get(r)
var buf bytes.Buffer
buf.WriteString("CURT Part ID,Customer Part ID, Price, Sale Start, Sale End\n")
for i := 0; i < len(prices)-1; i++ {
price := prices[i]
var priceBuf bytes.Buffer
priceBuf.WriteString(strconv.Itoa(price.PartID))
priceBuf.WriteByte(',')
priceBuf.WriteString(strconv.Itoa(price.CustPartID))
priceBuf.WriteByte(',')
priceBuf.WriteString(strconv.FormatFloat(price.Price, 'f', 2, 64))
priceBuf.WriteByte(',')
if price.IsSale == 1 {
priceBuf.WriteString(price.Sale_start.(string))
priceBuf.WriteByte(',')
priceBuf.WriteString(price.Sale_end.(string))
priceBuf.WriteByte(',')
}
priceBuf.WriteString("\n")
buf.Write(priceBuf.Bytes())
}
w.Header().Set("Content-Type", "text/csv")
w.Header().Set("Content-Disposition", "attachement;filename=data.csv")
w.Write(buf.Bytes())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment