[英]How to add column header to excel sheet?
private void btn_gnrt_files_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "")
{
//reading text file
FileInfo theSourceFile = new FileInfo(@"" + textBox1.Text);
StreamReader reader = theSourceFile.OpenText();
String filename = "";
String text = "";
do
{
text = reader.ReadLine();
if (text != null)
{
//MessageBox.Show(""+state);
String[] fname = text.Split('|'); //writiting file
if (state == true)
{
filename = fname[1];
filename.TrimStart();
//MessageBox.Show(filename);
Excel.Application excel = new Excel.Application();
excel.Visible = true;
//true is append parameter
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(@"" + textBox2.Text + @"\" + filename.TrimStart() + ".csv", true))
writer.WriteLine(text.Replace("|", ","));
I get excel files with data through this code. 我通过此代码获取具有数据的excel文件。 but i cant put column headers to excel sheets.
但我不能将列标题放在Excel工作表中。 plz tell me how to add these headers
请告诉我如何添加这些标题
<<reg no,br no,pr no,curency>>
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
System.Windows.MessageBox.Show("Excel is not properly installed!!");
return;
}
xlWorkBook = xlApp.Workbooks.Add();
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "reg no";
xlWorkSheet.Cells[1, 2] = "br no"
xlWorkSheet.Cells[1, 3] = "pr no"
xlWorkSheet.Cells[1, 4] = "curency";
And then do your business... 然后做你的事...
Column headers are just regular cells in Excel (or CSV), so add them as a first line. 列标题只是Excel(或CSV)中的常规单元格,因此将它们添加为第一行。
Add that header-line before you start the loop to write the data lines. 在开始循环以写入数据线之前,请添加该标题行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.