标题不会显示在我的网格上有HDR =是但没有

时间:2015-02-24 21:40:01

标签: c# vb.net export-to-excel

我有一个带有标题的excel文件,即使我在我的代码中设置了正确的参数HDR =是标题不会显示而是我的excel的第一行中的数据变成我的标题继承我的代码。

    private void button1_Click(object sender, EventArgs e)
    {


        OpenFileDialog FileDialog = new OpenFileDialog();
        FileDialog.CheckFileExists = true;
        FileDialog.CheckPathExists = true;
        FileDialog.Filter = "Excel |*.xlsx";
        FileDialog.InitialDirectory = @"C:\Users\carlosp";

        if (FileDialog.ShowDialog() == DialogResult.OK)
        {
            string filename;
            filename = FileDialog.FileName;

            System.Data.OleDb.OleDbConnection MyConnection;
            System.Data.DataSet DtSet;
            System.Data.OleDb.OleDbDataAdapter MyCommand;
            MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 12.0 Xml;HDR=YES';");
            MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Carlos$]", MyConnection);


                DtSet = new System.Data.DataSet();
                MyCommand.Fill(DtSet);
                dataGridView1.DataSource = DtSet.Tables[0];

                MyConnection.Close();
            }

    }

    private void button2_Click(object sender, EventArgs e)
    {
      string file_name = @"C:\Users\carlosp\test1.txt";

      var objWriter = new System.IO.StreamWriter(file_name);

      int count = dataGridView1.Rows.Count;

          for (int row = 0; row < count - 1; row++)
       {
           if (!string.IsNullOrWhiteSpace(row.ToString()))
            {
                objWriter.WriteLine(dataGridView1.Rows[row].Cells[0].Value.ToString() + "," + dataGridView1.Rows[row].Cells[1].Value.ToString() + "," + dataGridView1.Rows[row].Cells[2].Value.ToString());
            }

        }
      objWriter.Close();
    }


}

}

0 个答案:

没有答案