简体   繁体   English

在Excel导出文件中包含前导零

[英]Include leading zeros in excel export file

I have ac# windows application that lets a user to export data from a datagridview to an excel file. 我有一个ac#Windows应用程序,该应用程序允许用户将数据从datagridview导出到Excel文件。

My problem is that exported file does not include leading zeros in the cells. 我的问题是,导出的文件在单元格中不包含前导零。 So "00001" is displayed as "1". 因此,“ 00001”显示为“ 1”。

I tried putting single quote (') before the value to be written on the file but the quote is also displayed. 我尝试将单引号(')放在要写入文件的值之前,但还会显示引号。 '00001 '00001

Is there a way that the single quote not be displayed and it only displays 00001? 有没有一种方法可以不显示单引号,而只显示00001?

Here's the code that I used in writing to the file. 这是我在写入文件时使用的代码。

  private void btn_Export_Click(object sender, EventArgs e)
    {
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Excel Documents (*.xls)|*.xls";
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            // ToCsV(dataGridView1, @"c:\temp\export.xls");   ???????
            ToCsV(dgv_SLAccounts, sfd.FileName);
            MessageBox.Show("Successfully Created " + sfd.FileName);
        }  
    }

     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 + "\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 + "\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();
    }

Add \\t before your string. 在字符串前添加\\ t。 Problem solved. 问题解决了。

Excel will not display leading zeroes in CSV files. Excel将不会在CSV文件中显示前导零。 Your only option is to write the file in XLS or XLSX format, and then format the range containing the leading-zero cells as text. 您唯一的选择是以XLS或XLSX格式写入文件,然后将包含前导零单元格的范围格式化为文本。

Leading zeroes can indeed be preserved in CSV files when using Excel at least. 至少在使用Excel时,确实可以在CSV文件中保留前导零。 You were also pretty close to the working solution in trying to use the single quote in front of the number. 在尝试使用数字前面的单引号时,您也非常接近有效的解决方案。

However, the solution is to write the number with leading zeroes as a function: 但是,解决方案是将带前导零的数字写为函数:

="00001"

This way, Excel will interpret the value as a string and will not strip out the leading zeroes. 这样,Excel会将值解释为字符串,并且不会去除前导零。

Try this code (adapted from your original): 尝试以下代码(改编自原始代码):

stLine = stLine.ToString() +"=\""+ Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\"\t";

Use a format string when writing out your integer like so: 像这样写整数时,请使用格式字符串:

int i = 1; 
Console.WriteLine(i.ToString("00000"));

More info on custom numeric format strings here: http://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx 有关自定义数字格式字符串的更多信息,请参见: http : //msdn.microsoft.com/zh-cn/library/0c899ak8(v=vs.110).aspx

 int i=1;        
 Console.WriteLine(i.ToString().PadLeft(5,'0'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM