5
我想通過使用C#將一個很長的文本添加到Excel工作表中。我用這個代碼:使用C#將文本添加到Excel工作表中
worksheet.Cells[1, 1] = textString;
其結果是在這裏:
我要的是:
建議?
我想通過使用C#將一個很長的文本添加到Excel工作表中。我用這個代碼:使用C#將文本添加到Excel工作表中
worksheet.Cells[1, 1] = textString;
其結果是在這裏:
我要的是:
建議?
爲了獲得這種效果,您必須將文本複製到剪貼板,然後將其粘貼到相關的單元格中。看到這個例子
注意:將textString
設置爲你很長的字符串。
嘗試和測試上換行符
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();
// }
//}
}
}
快照
拆分並將所得陣列的每個元素添加到新的一行。 – 2012-08-05 04:18:08