| |

VerySource

 Forgot password?
 Register
Search
View: 1314|Reply: 12

Follow-up problems after DataGridView is imported into Excel

[Copy link]

2

Threads

8

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 United States

Post time: 2020-2-10 12:30:02
| Show all posts |Read mode
After I import the DataGridView data into Excel using the following method:
Because some of my field values ​​are all digits and start with 0, Excel will treat them as numbers by default, which will remove the leading 0. Can you set all Excel cell types to text? ?
SaveFileDialog saveFileDialog = new SaveFileDialog ();
            saveFileDialog.Filter = "Execl files (* .xls) | * .xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "Export Excel file to";

            saveFileDialog.ShowDialog ();

            if (saveFileDialog.FileName! = "")
            {
                Stream myStream;
                myStream = saveFileDialog.OpenFile ();
                StreamWriter sw = new StreamWriter (myStream, System.Text.Encoding.GetEncoding ("gb2312"));
                string str = "";
                try
                {
                    // write title
                    for (int i = 0; i <this.sc_madanDataGridView.ColumnCount; i ++)
                    {
                        if (i> 0)
                        {
                            str + = "\t";
                        }
                        str + = sc_madanDataGridView.Columns [i] .HeaderText;
                    }

                    sw.WriteLine (str);
                    // Write content
                    for (int j = 0; j <sc_madanDataGridView.Rows.Count; j ++)
                    {
                        string tempStr = "";
                        for (int k = 0; k <sc_madanDataGridView.Columns.Count; k ++)
                        {
                            if (k> 0)
                            {
                                tempStr + = "\t";
                            }
                            tempStr + = sc_madanDataGridView.Rows [j] .Cells [k] .FormattedValue.ToString ();
                        }
                        sw.WriteLine (tempStr);
                    }
                    sw.Close ();
                    myStream.Close ();
                }
                catch
                {
                    MessageBox.Show (e.ToString ());
                }
                finally
                {
                    sw.Close ();
                    myStream.Close ();
                }
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 Brazil

Post time: 2020-4-11 13:15:01
| Show all posts
The format of the EXCEL file can be individually set to text. If you ca n’t refer to COM for settings due to version issues, please search online and refer to the late binding method to call Selection.NumberFormatLocal = "@" in EXCEL or you can add "@" in front of all data. I do n’t know what the effect will be, hehe ~~~
Reply

Use magic Report

2

Threads

8

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-8-1 17:30:01
| Show all posts
Adding "@" won't work
Reply

Use magic Report

0

Threads

5

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-12 09:15:01
| Show all posts
Because it uses stream to read and write instead of com components, other methods will not work. Add "'" single quotes before the data to be processed.
Reply

Use magic Report

2

Threads

8

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-8-12 09:45:01
| Show all posts
Because it uses stream to read and write, not com components


Yes, but after I added single quotes, I output it directly in Excel, which didn't work.
tempStr +="'" + sc_madanDataGridView.Rows[j].Cells[k].FormattedValue.ToString();
Reply

Use magic Report

0

Threads

5

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-12 10:30:01
| Show all posts
Owner, please describe the problem clearly. You can create a new excel file and see the difference between "'01234" and "01234" in a cell
Reply

Use magic Report

0

Threads

5

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-12 11:00:02
| Show all posts
tempStr += sc_madanDataGridView.Rows[j].Cells[k].FormattedValue.ToString();
then
sw.WriteLine(tempStr);
Write in this way, do you write a line into one cell or multiple cells?
Reply

Use magic Report

2

Threads

8

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-8-12 11:15:01
| Show all posts
for (int k = 0; k <sc_madanDataGridView.Columns.Count; k++)
   {
      if (k> 0)
        {
            tempStr += "\t";
        }
     tempStr +="'" + sc_madanDataGridView.Rows[j].Cells[k].FormattedValue.ToString();
   }
sw.WriteLine(tempStr);

The more complete is like this. Now writing in Excel shows "'001001", when double-clicking the cell, "'" disappears
Reply

Use magic Report

0

Threads

5

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-12 11:30:01
| Show all posts
Isn't that the result you want?
Reply

Use magic Report

2

Threads

8

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-8-12 12:00:01
| Show all posts
But when displaying "'001001", you need to double-click each cell to hide "'"
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list