使用c#将文本添加到Excel表中

时间:2022-02-20 20:53:37

I'm trying to add a pretty long text into the Excel sheet by using C#. I use this code:

我正在尝试使用c#向Excel表中添加一个很长的文本。我用这段代码:

worksheet.Cells[1, 1] = textString;

The result is here: 使用c#将文本添加到Excel表中

其结果是:

What I want is: 使用c#将文本添加到Excel表中

我想要的是:

Suggestions?

建议吗?

1 个解决方案

#1


10  

To get this effect you have to copy the text to clipboard and then paste it to the relevant cell. See this example

要获得这种效果,您必须将文本复制到剪贴板,然后将其粘贴到相关的单元格。看这个例子

Note: Set textString to your very long string.

注意:将textString设置为非常长的字符串。

TRIED AND TESTED

尝试和测试

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //~~> Change Your String here
            String textString = "I'm trying to add a pretty long text into the Excel sheet by using sheet. I use this code:" + Environment.NewLine +
                               "worksheet.Cells[1, 1] = textString;" + Environment.NewLine +
                               "The result is here:";

            Clipboard.SetText(textString);

            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 Excel.Application();
            xlexcel.Visible = true;

            //~~> Add a new a workbook
            xlWorkBook = xlexcel.Workbooks.Add(misValue);

            //~~> Set Sheet 1 as the sheet you want to work with
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            //~~> Set your range
            Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];

            CR.Select();

            xlWorkSheet.Paste(CR, false);

            // xlWorkBook.Close(true, misValue, misValue);
            //  xlexcel.Quit();

            // releaseObject(xlWorkSheet);
            // releaseObject(xlWorkBook);
            // releaseObject(xlexcel);
        }

        //private void releaseObject(object obj)
        //{
        //    try
        //    {
        //        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        //        obj = null;
        //    }
        //    catch (Exception ex)
        //    {
        //        obj = null;
        //        MessageBox.Show("Unable to release the Object " + ex.ToString());
        //    }
        //    finally
        //    {
        //        GC.Collect();
        //    }
        //} 
    }
}

SNAPSHOT

快照

使用c#将文本添加到Excel表中

#1


10  

To get this effect you have to copy the text to clipboard and then paste it to the relevant cell. See this example

要获得这种效果,您必须将文本复制到剪贴板,然后将其粘贴到相关的单元格。看这个例子

Note: Set textString to your very long string.

注意:将textString设置为非常长的字符串。

TRIED AND TESTED

尝试和测试

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //~~> Change Your String here
            String textString = "I'm trying to add a pretty long text into the Excel sheet by using sheet. I use this code:" + Environment.NewLine +
                               "worksheet.Cells[1, 1] = textString;" + Environment.NewLine +
                               "The result is here:";

            Clipboard.SetText(textString);

            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 Excel.Application();
            xlexcel.Visible = true;

            //~~> Add a new a workbook
            xlWorkBook = xlexcel.Workbooks.Add(misValue);

            //~~> Set Sheet 1 as the sheet you want to work with
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            //~~> Set your range
            Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];

            CR.Select();

            xlWorkSheet.Paste(CR, false);

            // xlWorkBook.Close(true, misValue, misValue);
            //  xlexcel.Quit();

            // releaseObject(xlWorkSheet);
            // releaseObject(xlWorkBook);
            // releaseObject(xlexcel);
        }

        //private void releaseObject(object obj)
        //{
        //    try
        //    {
        //        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        //        obj = null;
        //    }
        //    catch (Exception ex)
        //    {
        //        obj = null;
        //        MessageBox.Show("Unable to release the Object " + ex.ToString());
        //    }
        //    finally
        //    {
        //        GC.Collect();
        //    }
        //} 
    }
}

SNAPSHOT

快照

使用c#将文本添加到Excel表中