简体   繁体   English

streamwriter:CSV文件

[英]streamwriter : csv file

I use StreamWriter to write data to a csv file. 我使用StreamWriter将数据写入csv文件。 When I open the csv file using Excel, everything is fine except that some of the cells have the hash symbol instead of the data written to the cell. 当我使用Excel打开csv文件时,一切都很好,除了某些单元格具有哈希符号,而不是写入单元格的数据。 This data, however, becomes visible on increasing the cell size. 但是,此数据在增加像元大小时变得可见。 Is there any way to overcome this problem of the hash( # ) symbols without having to manually increase cell sizes. 有什么方法可以克服哈希( # )符号的问题,而不必手动增加像元大小。

int cols;
//open file 
//StreamWriter wr = new StreamWriter(@"O:\C_2013_MRO_Software Database\Manpowerdata.csv");
StreamWriter wr = new StreamWriter(@"C:\Manpowerdata.csv");

//determine the number of columns and write columns to file 
var dgvStock = dataGridView3;
cols = dgvStock.Columns.Count;
for (int i = 0; i < cols - 1; i++)
{
    wr.Write(dgvStock.Columns[i].Name.ToString().ToUpper() + ",");
}
wr.WriteLine();

//write rows to excel file
for (int i = 0; i < (dgvStock.Rows.Count - 1); i++)
{
    for (int j = 0; j < cols; j++)
    {
        if (dgvStock.Rows[i].Cells[j].Value != null)
        {
            wr.Write(dgvStock.Rows[i].Cells[j].Value + ",");
        }
        else
        {
            wr.Write(",");
        }
    }
    wr.WriteLine();
}

//close file
wr.Close();

When viewed in MS Excel (and other spreadsheet programs), data which does not fit in a cell will be replaced with ######## . 在MS Excel(和其他电子表格程序)中查看时,单元格中不适合的数据将替换######## This does not indicate anything about the underlying CSV data. 这并不表示有关基础CSV数据的任何内容。

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

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