aspnet如何导出excel,aspnet导出excel几种方式
成都创新互联公司是一家集网站建设,威信企业网站建设,威信品牌网站建设,网站定制,威信网站建设报价,网络营销,网络优化,威信网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
创新互联专注于企业成都营销网站建设、网站重做改版、勉县网站定制设计、自适应品牌网站建设、H5技术、商城网站定制开发、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为勉县等各大城市提供网站开发制作服务。
1,aspnet导出excel几种方式
function initxlsobj(){ try { if (oxl == null) oxl = new activexobject("excel.application"); } catch(e) { alert("必须安装excel电子表格软件,同时浏览器安全设置:ativex控件和插件->对没有标记为安全的activex控件进行初始化和脚本运行,必须启用"); return ""; } owb = oxl.workbooks.add; activesheet = owb.worksheets(1); } function fulldata() { 相应处理方法。。。。。。。。。。。 activesheet.rows("1:1").font.size=14; activesheet.rows("1:1").font.bold=true; activesheet.columns.autofit; oxl.visible = true; } function endxlsobj(){ oxl.usercontrol = true;//excel交由用户控制 //owb.close(savechanges=false); //这句会关闭sheet //oxl.quit(); //这句会提示关闭生成的excel文件 oxl=null; owb=null; activesheet=null; }2,aspnet 导出excel哪种方法最好
最简单的方法是直接在后台cs文件里加上Response.ContentType = "application/vnd.ms-excel";这句话就可以了,你试试,决对会给你一个惊喜的public partial class _Default : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) } private void BindData(DataTable dt) repData.DataSource = dt; repData.DataBind(); } protected void btnLoadDt_Click(object sender, EventArgs e) BindData(LoadDataTable()); } //创建DataTable private DataTable LoadDataTable() DataTable dt = new DataTable(); DataColumn dc = new DataColumn("userName"); dt.Columns.Add(dc); dc = new DataColumn("userSex"); dt.Columns.Add(dc); DataRow dr = dt.NewRow(); dr[0] = "fengyan"; dr[1] = "male"; dt.Rows.Add(dr); dr = dt.NewRow(); dr[0] = "efly"; dr[1] = "male"; dt.Rows.Add(dr); dr = dt.NewRow(); dr[0] = "楚旋"; dr[1] = "male"; dt.Rows.Add(dr); dr = dt.NewRow(); dr[0] = "51aspx"; dr[1] = "male"; dt.Rows.Add(dr); return dt; } protected void btnExportExcel_Click(object sender, EventArgs e) //得到需要导入Excel的DataTable DataTable dt = LoadDataTable(); //将其列名添加进去! (这一步注意是为了方便以后将该Excel导入内存表中 自动创建列名用。) //DataRow dr = dt.NewRow(); //dr[0] = "userName"; //dr[1] = "userSex"; //dt.Rows.InsertAt(dr, 0); //实例化一个Excel助手工具类 ExcelHelper ex = new ExcelHelper(); //导入所有!(从第一行第一列开始) ex.DataTableToExcel(dt, 1, 1); //导出Excel保存的路径! ex.OutputFilePath = txtExcelPath.Text; ex.OutputExcelFile(); } protected void btnExcelToDataTable_Click(object sender, EventArgs e) string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtFromExcel.Text + ";Extended Properties=Excel 8.0"; //链接Excel OleDbConnection cnnxls = new OleDbConnection(strConn); //读取Excel里面有 表Sheet1 OleDbDataAdapter oda = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls); DataSet ds = new DataSet(); //将Excel里面有表内容装载到内存表中! oda.Fill(ds); DataTable dt = ds.Tables[0]; BindData(dt); }}3,求aspnet导出excel表的高效方法
public void ExportTOExcel() if (dbgname.Rows.Count == 0) MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Execl files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; saveFileDialog.CreatePrompt = true; saveFileDialog.Title = "导出文件保存路径"; saveFileDialog.ShowDialog(); string strName = saveFileDialog.FileName; if (strName.Length != 0) ToolStripProgressBar toolStripProgressBar1 = new ToolStripProgressBar(); toolStripProgressBar1.Visible = true; System.Reflection.Missing miss = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass(); excel.Application.Workbooks.Add(true); ; excel.Visible = false;//若是true,则在导出的时候会显示EXcel界面。 if (excel == null) MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks; Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss)); Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet; sheet.Name = "test"; //生成字段名称 for (int i = 0; i < dbgname.ColumnCount-8; i++) excel.Cells[1, i + 1] = dbgname.Columns[i].HeaderText.ToString(); } //填充数据 for (int i = 0; i < dbgname.RowCount - 1; i++) for (int j = 0; j < dbgname.ColumnCount - 8; j++) if (dbgname[j, i].Value == typeof(string)) excel.Cells[i + 2, j + 1] = "" + dbgname[i, j].Value.ToString(); } else excel.Cells[i + 2, j + 1] = dbgname[j, i].Value.ToString(); } } toolStripProgressBar1.Value += 100 / dbgname.RowCount; } sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss); book.Close(false, miss, miss); books.Close(); excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(book); System.Runtime.InteropServices.Marshal.ReleaseComObject(books); System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); GC.Collect(); MessageBox.Show("数据已经成功导出到:" + saveFileDialog.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information); toolStripProgressBar1.Value = 0; toolStripProgressBar1.Visible = false; } } }使用SQL的bcp命令,传递SQL语句(其中包含表名,列名,文件位置等)。将文件生成到一个专门的临时目录中,再用file类读取出来供用户下载。 这是一个思路,愿与你继续交流具体的实现代码。因为我也有这样的几万行下载的需求。4,aspnet中如何导出Excel表
public void createexcel(dataset ds,string typeid,string filename) httpresponse resp; resp = page.response; resp.contentencoding = system.text.encoding.getencoding("gb2312"); resp.appendheader("content-disposition", "attachment;filename=" + filename); string colheaders= "", ls_item=""; int i=0;//定义表对象与行对像,同时用dataset对其值进行初始化 datatable dt=ds.tables[0]; datarow[] myrow=dt.select(""); // typeid=="1"时导出为excel格式文件;typeid=="2"时导出为xml格式文件 if(typeid=="1") //取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符 for(i=0;icolheaders+=dt.columns[i].caption.tostring()+"\t"; colheaders +=dt.columns[i].caption.tostring() +"\n"; //向http输出流中写入取得的数据信息 resp.write(colheaders); //逐行处理数据 foreach(datarow row in myrow) { //在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n for(i=0;i ls_item +=row[i].tostring() + "\t"; ls_item += row[i].tostring() +"\n"; //当前行数据写入http输出流,并且置空ls_item以便下行数据 resp.write(ls_item); ls_item=""; } } else { if(typeid=="2") { //从dataset中直接导出xml数据并且写到http输出流中 resp.write(ds.getxml()); } } //写缓冲区中的数据到http头文件中 resp.end(); }使用Aspose.Cells.dll 插件,可以不让服务器安装Office软件就能操作Excel(读写修改等)代码发到那? /// /// 导出DataGridView的数据到Excel表中 /// /// public static void DataToExcel(DataGridView m_DataView) SaveFileDialog kk = new SaveFileDialog(); kk.Title = "保存EXECL文件"; kk.Filter = "EXECL文件(*.xls) |*.xls|所有文件(*.*) |*.*"; kk.FilterIndex = 1; if (kk.ShowDialog() == DialogResult.OK) string FileName = kk.FileName;// + ".slsx"; if (File.Exists(FileName)) File.Delete(FileName); 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 < m_DataView.Columns.Count; i++) if (m_DataView.Columns[i].Visible == true) strLine = strLine + m_DataView.Columns[i].HeaderText.ToString() + Convert.ToChar(9); } } objStreamWriter.WriteLine(strLine); strLine = ""; for (int i = 0; i < m_DataView.Rows.Count; i++) if (m_DataView.Columns[0].Visible == true) if (m_DataView.Rows[i].Cells[0].Value == null) strLine = strLine + " " + Convert.ToChar(9); else strLine = strLine + m_DataView.Rows[i].Cells[0].Value.ToString() + Convert.ToChar(9); } for (int j = 1; j < m_DataView.Columns.Count; j++) if (m_DataView.Columns[j].Visible == true) if (m_DataView.Rows[i].Cells[j].Value == null) strLine = strLine + " " + Convert.ToChar(9); else string rowstr = ""; rowstr = m_DataView.Rows[i].Cells[j].Value.ToString(); if (rowstr.IndexOf("\r\n") > 0) rowstr = rowstr.Replace("\r\n", " "); if (rowstr.IndexOf("\t") > 0) rowstr = rowstr.Replace("\t", " "); strLine = strLine + rowstr + Convert.ToChar(9); } } } objStreamWriter.WriteLine(strLine); strLine = ""; } objStreamWriter.Close(); objFileStream.Close(); } }调用Main_BodyExcel.DataToExcel(gwSum);
文章名称:aspnet如何导出excel,aspnet导出excel几种方式
分享网址:
http://kswjz.com/article/epsods.html
扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流