/// 将数据填充到指定的excel表格中
/// </summary>
/// <param name="filepath"></param>
/// <param name="fileSavePath"></param>
/// <param name="ds"></param>
/// <returns></returns>
public void DataSetToExcel(string filepath, string fileSavePath, DataSet ds)
{
Microsoft.Office.Interop.Excel.Application excelapp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = null;
workbook = excelapp.Workbooks.Open(filepath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet ThisSheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
ThisSheet.Cells[2, 3] =this.lblAppDate.Text;//时间
ThisSheet.Cells[2, 9] =this.lblAppOrderNo.Text;//编号
ThisSheet.Cells[3, 3] =this.txtCostNo.Text;//费用编号
ThisSheet.Cells[3, 5] =this.ddlObjectNo.SelectedItem.Text;//项目名称
ThisSheet.Cells[3, 9] = this.lblEmployeesName.Text;//负责人
ThisSheet.Cells[4, 3] = this.TextBox1.Text;//签订日期
ThisSheet.Cells[4, 5] = this.ddlCompanyNo.SelectedItem.Text;//所属公司
ThisSheet.Cells[4, 9] = this.ddlDealStatus.SelectedItem.Text;//合同状态
for (int i = 0; i <6;i++ )
{
ThisSheet.Cells[i + 6,1] = ((Label)FindControl("lblNum" + i)).Text;
if (((DropDownList)FindControl("ddlProductNo" + i)).SelectedItem.Text == "--请选择--")
{
ThisSheet.Cells[i + 6, 2] = "";
}
else
{
ThisSheet.Cells[i + 6, 2] = ((DropDownList)FindControl("ddlProductNo" + i)).SelectedItem.Text;
}
ThisSheet.Cells[i + 6, 3] = ((Label)FindControl("lblStandard" + i)).Text;
ThisSheet.Cells[i + 6, 4] = ((TextBox)FindControl("txtUnit" + i)).Text;
ThisSheet.Cells[i + 6, 5] = ((TextBox)FindControl("txtCount" + i)).Text;
if (((DropDownList)FindControl("ddlSupplierNo" + i)).SelectedItem.Text=="--请选择--")
{
ThisSheet.Cells[i + 6, 6] = "";
}
else
{
ThisSheet.Cells[i + 6, 6] = ((DropDownList)FindControl("ddlSupplierNo" + i)).SelectedItem.Text;
}
ThisSheet.Cells[i + 6, 7] = ((Label)FindControl("lblPrice" + i)).Text;
ThisSheet.Cells[i + 6, 8] = ((Label)FindControl("lblallMoney" + i)).Text;
ThisSheet.Cells[i + 6, 9] = ((TextBox)FindControl("lblDelevery" + i)).Text;
ThisSheet.Cells[i + 6, 10] = ((TextBox)FindControl("lblAppMemo" + i)).Text;
}
ThisSheet.Cells[12, 3] = this.lblAllMoney.Text;
ThisSheet.Cells[12, 8] = this.lblMoneyByObj.Text;
ThisSheet.Cells[14, 3] = this.lblAppEmpName.Text;
ThisSheet.Cells[15, 3] = this.lblBuyer.Text;
ThisSheet.Cells[15, 6] = this.lblIntegratedManager.Text;
excelapp.Visible = false;
try
{
workbook.Close(true, fileSavePath, System.Reflection.Missing.Value);
}
catch (Exception e)
{
throw e;
}
excelapp.Quit();
}
/// <summary>
/// 提供下载
/// </summary>
/// <param name="path"></param>
/// <param name="page"></param>
/// <param name="isDelete"></param>
public static void UploadExcel(string path, System.Web.UI.Page page, bool isDelete)
{
System.IO.FileInfo file = new System.IO.FileInfo(path);
page.Response.Clear();
page.Response.Charset = "GB2312";
page.Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
page.Response.AddHeader("Content-Disposition", "attachment; filename=" + page.Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
page.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
page.Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
page.Response.WriteFile(file.FullName);
page.Response.Flush();
if (isDelete)
{
System.IO.File.Delete(path);
}
// 停止页面的执行
page.Response.End();
}
/// <summary>
/// 导出按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnD_Click(object sender, EventArgs e)
{
string filepath = Server.MapPath("~") + @"\Upload\采购申请单.xls";
DataSetToExcel(filepath, @"E:\采购申请单.xls", null); //将其存在客户端
UploadExcel(@"E:\采购申请单.xls", this.Page, true);//下载客户端的excel
}
注:Microsoft.Office.Interop.Excel需引用;在发布的时候需DCOM组件中的Microsoft Excel Application标识改成交互式