方法一:如何将已做好的Excel模板嵌入到网页中,再从数据库中读取数据填充到Excel模板中
打开excel模板,通过数据集赋值给excel单元格。
Excel.Application app=new Application();
Excel._Workbook book;
Excel._Worksheet sheet;
book=(Excel._Workbook)app.Workbooks.Open(strPath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,
Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
sheet=(Excel._Worksheet)book.Sheets[1];
int j=dvs.Count;
Excel.Range ran1=app.ActiveCell;
_Range(sheet.Cells[1,1],sheet.Cells[1,9]);
ran1.Value2=Name;
for(int i=0;i<dvs.Count;i++)
{
Excel.Application app=new Application();
Excel._Workbook book;
Excel._Worksheet sheet;
book=(Excel._Workbook)app.Workbooks.Open(strPath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,
Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
sheet=(Excel._Worksheet)book.Sheets[1];
int j=dvs.Count;
Excel.Range ran1=app.ActiveCell;
_Range(sheet.Cells[1,1],sheet.Cells[1,9]);
ran1.Value2=Name;
for(int i=0;i<dvs.Count;i++)
{
try
{
s=Convert.ToString(i);
sheet.Cells[i+4,"A"]=dvs[i]["BH"].ToString().Trim();
}
catch(Exception ex)
{
HttpContext.Current.Response.Write("<script language='javascript'>alert('"+ex.Message+"')</script>");
book.Close(null,null,null);
app.Workbooks.Close();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
{
s=Convert.ToString(i);
sheet.Cells[i+4,"A"]=dvs[i]["BH"].ToString().Trim();
}
catch(Exception ex)
{
HttpContext.Current.Response.Write("<script language='javascript'>alert('"+ex.Message+"')</script>");
book.Close(null,null,null);
app.Workbooks.Close();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
sheet=null;
book=null;
app=null;
GC.Collect();
HttpContext.Current.Response.Write("<script language='javascript'>alert('导出失败!')</script>");
return;
}
}
book.Save();
book.Close(null,null,null);
如何做网页app.Workbooks.Close();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
book=null;
app=null;
GC.Collect();
HttpContext.Current.Response.Write("<script language='javascript'>alert('导出失败!')</script>");
return;
}
}
book.Save();
book.Close(null,null,null);
如何做网页app.Workbooks.Close();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
sheet=null;
book=null;
app=null;
GC.Collect();
GC.Collect();
GC.Collect();
HttpContext.Current.Response.Write("<script language='javascript'>alert('导出成功!')</script>");
HttpContext.Current.Response.Write("<script language='javascript'>window.open('../Template_temp/A.xls','_bank')</script>");
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
sheet=null;
book=null;
app=null;
GC.Collect();
GC.Collect();
GC.Collect();
HttpContext.Current.Response.Write("<script language='javascript'>alert('导出成功!')</script>");
HttpContext.Current.Response.Write("<script language='javascript'>window.open('../Template_temp/A.xls','_bank')</script>");
方法二、关于网页中嵌入excel表格
从Excel中取得数据 string ConnStr =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + file +
";Extended Properties=Excel 8.0; ";
OleDbConnection con = new OleDbConnection(ConnStr);
OleDbCommand com = new OleDbCommand( "SELECT * FROM [sheet1$] where
"+StrSql, con);
OleDbDataAdapter ada = new OleDbDataAdapter(com);
DataSet ds=new DataSet();
DataTable dt = new DataTable();
";Extended Properties=Excel 8.0; ";
OleDbConnection con = new OleDbConnection(ConnStr);
OleDbCommand com = new OleDbCommand( "SELECT * FROM [sheet1$] where
"+StrSql, con);
OleDbDataAdapter ada = new OleDbDataAdapter(com);
DataSet ds=new DataSet();
DataTable dt = new DataTable();
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论