Click here to Skip to main content
15,899,025 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i use this code
C#
private void ToCsV(DataGridView dGV, string filename)
        {
            string stOutput = "";
            // Export titles:
            string sHeaders = "";

            for (int j = 0; j < dGV.Columns.Count; j++)
                sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
            stOutput += sHeaders + "\r\n";
            // Export data.
            for (int i = 0; i < dGV.RowCount - 1; i++)
            {
                string stLine = "";
                for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
                    stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t";
                stOutput += stLine + "\r\n";
            }
            Encoding utf16 = Encoding.GetEncoding(1254);
            byte[] output = utf16.GetBytes(stOutput);
            FileStream fs = new FileStream(filename, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(output, 0, output.Length); //write the encoded file
            bw.Flush();
            bw.Close();
            fs.Close();
        }  

and this one in the button

C#
private void button6_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel Documents (*.xlsx)|*.xlsx";
            sfd.FileName = "export.xlsx";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                //ToCsV(dataGridView1, @"c:\export.xls");
                ToCsV(dataGridView1, sfd.FileName); // Here dataGridview1 is your grid view name
                MessageBox.Show("Excel Exported!");
            }
        }



but when i try to open the file it say the file is not in a right format

can anyone help please
Posted
Updated 7-Jan-14 2:39am
v2

You are getting somewhat mixed up here. You start by creating some tab separated strings, not the best idea for CSV (i.e. Comma Separated Values). You then use some strange encoding on your data and write it out to the file as a binary stream, but giving the file name a .xlsx extension. CSV files are pure text and should have each field separated by commas. The extension for the filename should be .csv so that Excel can correctly decode it.
 
Share this answer
 
As Richard said, you are not making a 'ToCSV' document in your code. It looks like you are trying to make an actual .xlsx file. You can't do it that way. If you follow Richard's advice you can make a .csv file that will open correctly in excel. This is usually sufficient for most needs.

However if you really want to create a real xlsx file you will need a lot more work.

Here's a link to give you some idea of what you are looking at.
How to Read and Write .xlsx (Excel 2007) file - Part I[^]
 
Share this answer
 
thank you richard and bowltuner

i will try to do it thank you for your advise

ps. i am just a beginner programming sry if i am to stupit about this :(
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-May-14 10:52am    
Not an answer. Such posts are considered as abuse. Instead, add comments to other posts (their authors will get notifications, but not in this case), and/or use "Improve question". And why did you even accept this post formally?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900