excel单元格换行是ALT+回车。我要写程序输出一些数据到excel,怎么实现单元格内换行?
\r
\r\n
\n
这三个都试了,只是显示一个空格,没有起到换行的效果

解决方案 »

  1.   

    你用的是模板还是什么?如果是自己定义的excel模板哪里面有个自动换行打上对勾就行了
      

  2.   

    是自己写字符串的形式生成.CSV文件
      

  3.   

    lz,可以试试\t,我导出数据到EXCEL,是能换行的
      

  4.   

    试了,\t不行,什么都不显示。
    比如我想在某单元格内这样显示:
    aa
    bb
    用了\t,显示成了:
    aabb
      

  5.   

    试试这段代码...
    public void OutExcel(DataGridView dataGV)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.CreatePrompt = true;
                saveFileDialog.Title = "Export Excel File To";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    Stream myStream = saveFileDialog.OpenFile();
                    StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
                    string str = "";
                    try
                    { //写标题
                        for (int i = 0; i < dataGV.ColumnCount; i++)
                        {
                            if (i > 0)
                            { str += "\t"; }
                            str += dataGV.Columns[i].HeaderText;
                        }
                        sw.WriteLine(str);
                        //写内容
                        for (int j = 0; j < dataGV.Rows.Count; j++)
                        {
                            string tempStr = "";
                            for (int k = 0; k < dataGV.ColumnCount; k++)
                            {
                                if (k > 0)
                                {
                                    tempStr += "\t";
                                }
                                tempStr += dataGV.Rows[j].Cells[k].Value.ToString().Trim();
                            }
                            sw.WriteLine(tempStr);
                        }
                        MessageBox.Show("导出成功!");
                        sw.Close();
                        myStream.Close();
                    }
                    catch
                    { MessageBox.Show("导出失败"); }
                    finally { sw.Close(); myStream.Close(); }
                }
            }
      

  6.   

    哦,不好意思,LZ我理解错你的意思了,/t不是用来换行的,是不让某行的内容全挤在1个单元格里
    只是换行的话,好像没什么台问题呀?
    最简单的例子saveFileDialog1.Filter = "Execl文件(*.xls)|*.xls";
                saveFileDialog1.RestoreDirectory = true;
                saveFileDialog1.Title = "导出文件Excel";
                string[] ss = new string[] { "aa", "bb" }; //换成你的数据
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string stt = saveFileDialog1.FileName;
                    string fileName = stt;
                    System.IO.File.WriteAllLines(fileName, ss, System.Text.Encoding.Default);
                    MessageBox.Show("导出以完成", "系统提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
                }你想要几行,只要数组定义几行就行了,然后/t能解决有秩序的把每个内容插入每行单元格内
      

  7.   

    StreamWriter objStreamWriter;
      objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
      objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);  objStreamWriter.WriteLine("<table><tr><td>" + "aa" + "<br style='mso-data-placement:same-cell;'/> " + "cc" + "<br style='mso-data-placement:same-cell;'/> " + "dd" + "</td><td>"+"pp"+"</td></tr></table>");range.WrapText = true