请教怎么才能在Web方式下打开Excel并可以填入数据,然后保存到数据库里面 请教怎么才能在Web方式下打开Excel并可以填入数据,然后保存到数据库里面我想在web页面中打开excel模板,并且可以往里面填入数据,根据公式计算,然后倒入到数据库中去,各位有没有详细代码,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我这有一点代码不知道对你有没有用首先要加入com 组件Microsoft.Excel 9.0 lib.... #region 打開模板文件寫入數據並進行垃圾回收 private string ReadAndWrite() { Excel.ApplicationClass excel = null; Excel.Workbooks workbooks; Excel.Workbook workbook; Excel.Worksheet worksheet; Excel.Sheets worksheets; excel = new Excel.ApplicationClass(); excel.Visible=true; workbooks = excel.Workbooks; workbook = workbooks.Open(Server.MapPath("./Templet.xls"),Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value); //模板文件 worksheets = (Excel.Sheets)excel.Worksheets; worksheet = (Excel.Worksheet)worksheets.get_Item("detail"); worksheet.Activate(); Missing miss=Missing.Value; for(int i=0;i<dtCost.Rows.Count;i++ ) { for(int j=0;j<dtCost.Columns.Count;j++) { excel.Cells[i+2,j+1]=dtCost.Rows[i][j].ToString(); } } string strFileName=Guid.NewGuid().ToString()+".xls"; workbook.RefreshAll(); workbook.SaveAs(Server.MapPath("./Temp/")+strFileName,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value,Missing.Value);//保存表格到臨時文件夾 #region 關閉進程及垃圾回收 workbook.Close(false,miss,miss); workbooks.Close(); excel.Quit(); //System.Runtime.InteropServices.Marshal.ReleaseComObject(); System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks); System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); GC.Collect(); #endregion return strFileName; } #endregion 你可以到http://ntko.com这个网站去看看 可以参见我写的:http://www.cnblogs.com/linfuguo/archive/2006/03/23/357155.html Calendar1如何获取数据库里日期并选择 谢谢大家解答 .net网站发布在Linux服务器下实现压缩文件上传后自己解压的功能 (安全)asp.net页面传参 关于LINQ to SQL 相关问题 数据更新不起来 ASP.Net如何监视某文件夹 gridview问题 请各位帮忙,关于分类汇总,谢谢! <select></select>如何读取数据 急了一下午啦 不用table,能使页面的所有控件居中显示吗 怎么样可以把URL地址的后面的后缀名去掉啊。在线等!!!!!!!!!!!!!!!!!!!! 求一数据表映射到实体类,然后绑定到datagrid的例子
不知道对你有没有用
首先要加入com 组件Microsoft.Excel 9.0 lib....
#region 打開模板文件寫入數據並進行垃圾回收
private string ReadAndWrite()
{
Excel.ApplicationClass excel = null;
Excel.Workbooks workbooks;
Excel.Workbook workbook;
Excel.Worksheet worksheet;
Excel.Sheets worksheets;
excel = new Excel.ApplicationClass();
excel.Visible=true;
workbooks = excel.Workbooks;
workbook = workbooks.Open(Server.MapPath("./Templet.xls"),Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value); //模板文件
worksheets = (Excel.Sheets)excel.Worksheets;
worksheet = (Excel.Worksheet)worksheets.get_Item("detail");
worksheet.Activate();
Missing miss=Missing.Value; for(int i=0;i<dtCost.Rows.Count;i++ )
{
for(int j=0;j<dtCost.Columns.Count;j++)
{
excel.Cells[i+2,j+1]=dtCost.Rows[i][j].ToString();
}
} string strFileName=Guid.NewGuid().ToString()+".xls";
workbook.RefreshAll();
workbook.SaveAs(Server.MapPath("./Temp/")+strFileName,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value,Missing.Value);//保存表格到臨時文件夾 #region 關閉進程及垃圾回收
workbook.Close(false,miss,miss);
workbooks.Close();
excel.Quit();
//System.Runtime.InteropServices.Marshal.ReleaseComObject();
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();
#endregion
return strFileName; }
#endregion
http://www.cnblogs.com/linfuguo/archive/2006/03/23/357155.html