[英]writing to excel file with text template in C#
我想編寫一個模板文件(* .tt),以將數據寫入C#中的XLS文件。 如何在單獨的列中寫入數據? 例如我想要一個3列的excel文件,如下所示
column1 column2 column3
sss ttt rrr
www qqq aaa
但我不能將它們插入單獨的列中
<#= "sss " #><#= "ttt " #><#= "," #><#= "rrr" #>
<#= "www " #><#= "qqq " #><#= "," #><#= "aaa" #>
和Excel文件中的輸出是這樣的
column1
sss ttt rrr
www qqq aaa
並將所有數據插入第一列
如果您選擇使用Excel Interop,則以下是將數據放在Excel工作表的單獨列中的演示。 您可以參考此以獲取有關Excel對象模型參考的更多詳細信息。
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelInterop
{
class Program
{
static void Main(string[] args)
{
Excel.Application xlApp = null;
Excel.Workbook xlWorkBook = null;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add();
Excel.Worksheet newWorksheet = null;
newWorksheet = (Excel.Worksheet)xlApp.Application.Worksheets.Add();
xlApp.ScreenUpdating = false;
Excel.Range excelRange = newWorksheet.UsedRange;
// Column 1
excelRange.Cells.set_Item(1, 1, "Column 1");
// Column 1 Data
excelRange.Cells.set_Item(2, 1, "sss");
// Column 2
excelRange.Cells.set_Item(1, 2, "Column 2");
// Column 1 Data
excelRange.Cells.set_Item(2, 2, "ttt");
// Save it as .xls
newWorksheet.SaveAs("D:\\ExcelInterop", Excel.XlFileFormat.xlExcel7);
// Clean up
xlWorkBook.Close();
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.