简体   繁体   中英

streamwriter : csv file

I use StreamWriter to write data to a csv file. 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. 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 ######## . This does not indicate anything about the underlying CSV data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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