dataGridView with line break, but when exporting to excel no break

Asked

Viewed 328 times

1

Galley,

I have a dataGridView that used the following code snippet to break the text:

     dataGridView1.Columns[0].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
     dataGridView1.Columns[1].DefaultCellStyle.WrapMode = DataGridViewTriState.True;

     dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
     dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
     dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;

On the grid is working correctly, example of the string I’m receiving:

"In the morning sun r n a dewdrop r n precious diamond."

When exporting to excel does not break text on the "SAME" cell and yes to the bottom line.

inserir a descrição da imagem aqui

Code to export to Excel

        private void button2_Click(object sender, EventArgs e)
    {
        copyAlltoClipboard();
        Microsoft.Office.Interop.Excel.Application xlexcel;
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;
        xlexcel = new Microsoft.Office.Interop.Excel.Application();
        xlexcel.Visible = true;
        xlWorkBook = xlexcel.Workbooks.Add(misValue);
        xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        Microsoft.Office.Interop.Excel.Range CR = (Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 1];
       // Qubra de linha
       // xlWorkSheet.Range["B1:B500"].Cells.WrapText = true;
        xlWorkSheet.Columns[2].Cells.WrapText = true;
        xlWorkSheet.Columns[2].ColumnWidth += 15;
        //xlWorkSheet.Columns[2].AutoFit();
        xlWorkSheet.Columns[2].Cells.WrapText = true;
        xlWorkSheet.Columns[2].Style.WrapText = true;
        xlWorkSheet.Cells[8, 2].Style.WrapText = true;
        xlWorkSheet.Cells[9, 2].Style.WrapText = true;
        xlWorkSheet.Cells[10, 2].Style.WrapText = true;
        xlWorkSheet.Columns[2].WrapText = true;
        //Set Text-Wrap for all rows true//
        xlWorkSheet.Rows.WrapText = true;
        CR.Select();
        xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);          
    }

As you can see when breaking text for excel, it is not broken within the same cell but for new lines. How can I fix this?

  • Wouldn’t be the case to take the WrapText?

  • \r\n will in fact break the line to another cell in excel, I think you can give a replace by <br> or only by \n

  • Excel works with pre-formatting, in this case you need to edit the column that will receive this data with the option "Break text automatically", so regardless of how it comes from your code the same will respect the established conditions of the worksheet, or in this case, the column.

  • @Leandro had already tried with n, Newline;,(char)13(char)10; nothing works. " Igor" I can’t create spreadsheet before because these spreadsheets have to be created on time, I can’t have a preset before.

1 answer

-3

Only use the replace method to replace n with a
like: Replace(" n", "<"br">");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.