0

I have a java script that exports website html table contents to CSV from a Web Application. I would like to bypass that and when a button is pressed it runs a query and downloads it to CSV without first loading it to the html table. Does anyone have anything like that?

This is useful for printing out reports. Some big reports that i have since they have to load in the website first cause too much lag.

1 Answer 1

1

You would just do something like:

func(w http.ResponseWriter, r *http.Request)
    var data = [][]string{{"Line1", "Hello"}, {"Line2", "World"}}
    buffer := &bytes.Buffer{} // creates IO Writer

    writer := csv.NewWriter(buffer)

    for _, value := range data {
        err := writer.Write(value)
        checkError("Cannot write to buffer", err)
    }

    writer.Flush()

    w.Header().Set("Content-Type", "text/csv") // setting the content type header to text/csv
    w.Header().Set("Content-Disposition", "attachment;filename=TheCSVFileName.csv")
    w.Write(buffer.Bytes())
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.