《C# 导出ECEL几种方式.docx》由会员分享,可在线阅读,更多相关《C# 导出ECEL几种方式.docx(12页珍藏版)》请在三一办公上搜索。
1、C# 导出ECEL几种方式public void Export(DataGridView dataGridView1)/导出到execl try/没有数据的话就不往下执行 if (dataGridView1.Rows.Count = 0)return;/实例化一个Excel.Application对象 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application;/让后台执行设置为不可见,为true的话会看到打开一个Excel,然后数据在往里写 excel.Vi
2、sible = true;/新增加一个工作簿,Workbook是直接保存,不会弹出保存对话框,加上Application会弹出保存对话框,值为false会报错 excel.Application.Workbooks.Add(true);/生成Excel中列头名称 for (int i = 0; i < dataGridView1.Columns.Count; i+)excel.Cells1, i + 1 = dataGridView1.Columnsi.HeaderText;/把DataGridView当前页的数据保存在Excel中 for (int i = 0; i < dataGr
3、idView1.Rows.Count - 1; i+)for (int j = 0; j < dataGridView1.Columns.Count; j+)if (dataGridView1j, i.ValueType = typeof(string)excel.Cellsi + 2, j + 1 = + dataGridView1j, i.Value.ToString;elseexcel.Cellsi + 2, j + 1 = dataGridView1j, i.Value.ToString;/设置禁止弹出保存和覆盖的询问提示框 excel.DisplayAlerts = false;
4、excel.AlertBeforeOverwriting = false;/保存工作簿 excel.Application.Workbooks.Add(true).Save;/保存excel文件 excel.Save(D: + KKHMD.xls);/确保Excel进程关闭 excel.Quit;excel = null;catch (Exception ex)MessageBox.Show(ex.Message, 错误提示);/测试已成功public void DataToExcel(DataGridView m_DataView)SaveFileDialog kk = new SaveFi
5、leDialog;kk.Title = 保存EXECL文件;kk.Filter = EXECL文件(*.xls) |*.xls |所有文件(*.*) |*.*;kk.FilterIndex = 1;if (kk.ShowDialog = DialogResult.OK)string FileName = kk.FileName + .xls;if (File.Exists(FileName)File.Delete(FileName);FileStream objFileStream;StreamWriter objStreamWriter;string strLine = ;objFileSt
6、ream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);for (int i = 0; i < m_DataView.Columns.Count; i+)if (m_DataView.Columnsi.Visible = true)strLine = strLine + m_DataView.Columnsi.HeaderText.ToStri
7、ng + Convert.ToChar(9);objStreamWriter.WriteLine(strLine);strLine = ;for (int i = 0; i < m_DataView.Rows.Count; i+)if (m_DataView.Columns0.Visible = true)if (m_DataView.Rowsi.Cells0.Value = null)strLine = strLine + + Convert.ToChar(9);elsestrLine = strLine + m_DataView.Rowsi.Cells0.Value.ToString
8、+ Convert.ToChar(9);for (int j = 1; j < m_DataView.Columns.Count; j+)if (m_DataView.Columnsj.Visible = true)if (m_DataView.Rowsi.Cellsj.Value = null)strLine = strLine + + Convert.ToChar(9);elsestring rowstr = ;rowstr = m_DataView.Rowsi.Cellsj.Value.ToString;if (rowstr.IndexOf(rn) > 0)rowstr = ro
9、wstr.Replace(rn, );if (rowstr.IndexOf(t) > 0)rowstr = rowstr.Replace(t, );strLine = strLine + rowstr + Convert.ToChar(9);objStreamWriter.WriteLine(strLine);strLine = ;objStreamWriter.Close;objFileStream.Close;MessageBox.Show(this, 保存EXCEL成功, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information);/测
10、试已成功public void DataToExcel(DataTable m_DataTable)SaveFileDialog kk = new SaveFileDialog;kk.Title = 保存EXECL文件;kk.Filter = EXECL文件(*.xls) |*.xls |所有文件(*.*) |*.*;kk.FilterIndex = 1;if (kk.ShowDialog = DialogResult.OK)string FileName = kk.FileName + .xls;if (File.Exists(FileName)File.Delete(FileName);F
11、ileStream 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 < m_DataTable.Columns.Count; i+)strLine = strLi
12、ne + m_DataTable.Columnsi.Caption.ToString + Convert.ToChar(9);objStreamWriter.WriteLine(strLine);strLine = ;for (int i = 0; i < m_DataTable.Rows.Count; i+)for (int j = 0; j < m_DataTable.Columns.Count; j+)if (m_DataTable.Rowsi.ItemArrayj = null)strLine = strLine + + Convert.ToChar(9);elsestring
13、 rowstr = ;rowstr = m_DataTable.Rowsi.ItemArrayj.ToString;if (rowstr.IndexOf(rn) > 0)rowstr = rowstr.Replace(rn, );if (rowstr.IndexOf(t) > 0)rowstr = rowstr.Replace(t, );strLine = strLine + rowstr + Convert.ToChar(9);objStreamWriter.WriteLine(strLine);strLine = ;objStreamWriter.Close;objFileStre
14、am.Close;MessageBox.Show(this, 保存EXCEL成功, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information);/测试已成功public bool ExportDataGridview(DataGridView gridView, bool isShowExcle)if (gridView.Rows.Count = 0)return false;/建立Excel对象Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Inte
15、rop.Excel.Application;excel.Application.Workbooks.Add(true);excel.Visible = isShowExcle;/生成字段名称for (int i = 0; i < gridView.ColumnCount; i+)excel.Cells1, i + 1 = gridView.Columnsi.HeaderText;/填充数据for (int i = 0; i < gridView.RowCount - 1; i+)for (int j = 0; j < gridView.ColumnCount; j+)if (gri
16、dViewj, i.ValueType = typeof(string)excel.Cellsi + 2, j + 1 = + gridViewj, i.Value.ToString;elseexcel.Cellsi + 2, j + 1 = gridViewj, i.Value.ToString;return true;/web导出,已测试成功private void Export(string FileName)/以下三行可选,如果没有的话导出的只是当前页数据,没有其他页数据 gvShow.AllowPaging = false;gvShow.AllowSorting = true;Res
17、ponse.Charset = GB2312;Response.ContentEncoding = System.Text.Encoding.UTF7;Response.AppendHeader(Content-Disposition, attachment;filename= + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString);Response.ContentType = application/vnd.ms-excel;this.EnableViewState = false;StringWriter tw = new StringWriter;HtmlTextWriter hw = new HtmlTextWriter(tw);gvShow.RenderControl(hw);Response.Write(tw.ToString);Response.End;