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. but i cant put column headers to excel sheets. 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.
Add that header-line before you start the loop to write the data lines.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.