C#导出数据到excel

<textarea cols="50" rows="15" name="code" class="c-sharp">//Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); //xlApp.Application.Workbooks.Add(true); //MessageBox.Show("完成"); //int row = this.dgvItems.RowCount; //int col = this.dgvItems.ColumnCount; //for (int c = 0; c &lt; col; c++) //{ // xlApp.Cells[1, c + 1] = dgvItems.Columns[c].HeaderText.ToString(); //} //for (int r = 0; r &lt; row; r++) //{ // for (int c = 0; c &lt; col; c++) // { // xlApp.Cells[r + 2, c + 1] = dgvItems.Rows[r].Cells[c].Value.ToString(); // } //} //xlApp.Visible = true; //xlApp = null; string FileName = "d://" + cbxType.Text.ToString() + this.dtpBeginDate.Value.Date.ToString("yyyy-MM-dd") + "至" + this.dtpEndDate.Value.Date.ToString("yyyy-MM-dd") + ".xls"; FileStream objFileStream; StreamWriter objStreamWriter; string strLine = ""; objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write); objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode); for (int i = 0; i &lt; dgvItems.Columns.Count; i++) { strLine = strLine + dgvItems.Columns[i].HeaderText.ToString() + Convert.ToChar(9); } objStreamWriter.WriteLine(strLine); strLine = ""; for (int i = 0; i &lt; dgvItems.Rows.Count; i++) { strLine = strLine + (i + 1) + Convert.ToChar(9); for (int j = 1; j &lt; dgvItems.Columns.Count; j++) { strLine = strLine + dgvItems.Rows[i].Cells[j].Value.ToString() + Convert.ToChar(9); } objStreamWriter.WriteLine(strLine); strLine = ""; } objStreamWriter.Close(); objFileStream.Close(); MessageBox.Show("详单导出成功");</textarea>

你可能感兴趣的:(C#导出数据到excel)