Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将DataGridView导出到Excel而不更改格式_C#_Excel_Winforms_Date_Datagridview - Fatal编程技术网

C# 将DataGridView导出到Excel而不更改格式

C# 将DataGridView导出到Excel而不更改格式,c#,excel,winforms,date,datagridview,C#,Excel,Winforms,Date,Datagridview,我正在创建一个Windows应用程序,其中我将数据从DataGridView导出到Excel。问题是在导出数据时,数据格式会发生变化,例如网格中的数据格式是dd/mm/yyyyy,但它被导出为dd/mm/yyyy tt:mm,并且Excel中会出现一些类似2.10的数字2.1: private void button4_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFile

我正在创建一个Windows应用程序,其中我将数据从DataGridView导出到Excel。问题是在导出数据时,数据格式会发生变化,例如网格中的数据格式是
dd/mm/yyyyy
,但它被导出为
dd/mm/yyyy tt:mm
,并且Excel中会出现一些类似
2.10
的数字
2.1

        private void button4_Click(object sender, EventArgs e)
    {
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Excel Documents (*.csv)|*.csv";
        sfd.FileName = "GE.csv";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            //ToCsV(dataGridView1, @"c:\export.xls");
            ToCsV(dataGridView2, sfd.FileName); // Here dataGridview1 is your grid view name 


        }
        textBox3.Text = sfd.FileName;
        button5.Visible = true;
    }
    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();
    }  
private void按钮4\u单击(对象发送者,事件参数e)
{
SaveFileDialog sfd=新建SaveFileDialog();
sfd.Filter=“Excel文档(*.csv)|*.csv”;
sfd.FileName=“GE.csv”;
if(sfd.ShowDialog()==DialogResult.OK)
{
//ToCsV(dataGridView1,@“c:\export.xls”);
ToCsV(dataGridView2,sfd.FileName);//这里dataGridview1是您的网格视图名称
}
textBox3.Text=sfd.FileName;
按钮5.可见=真;
}
私有void ToCsV(DataGridView dGV,字符串文件名)
{
字符串stOutput=“”;
//出口名称:
字符串sHeaders=“”;
对于(int j=0;j
有什么办法吗

更新:


我已将DB上的日期文件DSS更改为varchar,它工作正常,但数字仍然会丢失最后一个小数如果它为零,我尝试将它提取到txt文件中,结果显示为零,但当我用excel打开文本时,它丢失了零

数据格式是excel本身的属性。您正在导出为csv,无法指定Excel的格式。当我将其作为文本打开时,它的格式与错误的格式相同,并试图将代码更改为sfd.Filter=“Excel文档(.xls)|.xls”;sfd.FileName=“GE.xls”;仍在更改格式我将数据库上的日期类型更改为varchar,虽然有效,但仍然存在dicemel问题