#region 导出
private void cmdExport_ServerClick(object sender, System.EventArgs e)
{
try
{
string DownloadPath=Server.MapPath("../DataFolder/FileExport"); //副本的文件夹路径。
//副本的文件名。
string TempFileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS";
this.txtTempFileName.Text=TempFileName;
object missing = System.Reflection.Missing.Value;
object missing2 = System.Reflection.Missing.Value;
Excel.Application myExcel=new Excel.Application();
Excel.Workbook myBook=(Excel.Workbook)myExcel.Workbooks.Add(missing);
Excel.Worksheet curSheet = (Excel.Worksheet)myBook.Sheets[1];
//设置Excel样式
Range r1=(Range)myExcel.Cells[1,2];
Range r2=(Range)myExcel.Cells[3,4];
//Range r10=(Range)myExcel.Cells[9,10];
//r10.AutoOutline();
//r10.FillLeft();
r1.Font.Bold=true;
r1.Font.Color=Information.RGB(0,255,0);
r2.Font.Bold=true;
string DownloadFilePath=DownloadPath+"//"+TempFileName;
DataSet ds=this.GetDataSet();
int rc=ds.Tables[0].Rows.Count;
//绘制边框
Range rBorders=(Range)curSheet.get_Range(myExcel.Cells[7,1],myExcel.Cells[7+rc+1,10]);
rBorders.Borders.LineStyle=1;
curSheet.get_Range(myExcel.Cells[7,1],myExcel.Cells[7+rc+1,1]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗
curSheet.get_Range(myExcel.Cells[7,1],myExcel.Cells[7,10]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置设置上边线加粗
curSheet.get_Range(myExcel.Cells[7+rc+1,1],myExcel.Cells[7+rc+1,10]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗
curSheet.get_Range(myExcel.Cells[7,10],myExcel.Cells[7+rc+1,10]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗
//Excel的表头信息
myExcel.Cells[1,2]=this.lblExcelHead.Text;
myExcel.Cells[2,2]="New Added:" + DateTime.Now.ToString();
myExcel.Cells[3,4]="VENDOR CODE LIST - BY PRODUCTS.";
myExcel.Cells[4,4]="****************************************";
myExcel.Cells[5,9]="DATE From:" + this.txtCreateDate.Text;
myExcel.Cells[6,9]="DATE To:" + this.txtDateNow.Text;
myExcel.Cells[7,5]="PARTS SUPPLIER";
//设置Excel表列头
myExcel.Cells[8,1]="Item";
myExcel.Cells[8,2]="OrgCode";
myExcel.Cells[8,3]="VendorCode";
myExcel.Cells[8,4]="VendorName";
myExcel.Cells[8,5]="A";
myExcel.Cells[8,6]="B";
myExcel.Cells[8,7]="C";
myExcel.Cells[8,8]="PayMentType";
myExcel.Cells[8,9]="TermsCode";
myExcel.Cells[8,10]="CreateTime";
//设置表头字体风格
curSheet.get_Range(myExcel.Cells[7,1],myExcel.Cells[8,10]).Font.Bold=true;
int j=1;//j为总结的Item数目的变量
int i=9;
while (i-8<=ds.Tables[0].Rows.Count)
{
myExcel.Cells[i,1]=j.ToString();
myExcel.Cells[i,2]=ds.Tables[0].Rows[i-9]["OrgCode"].ToString().Trim();
myExcel.Cells[i,3]=ds.Tables[0].Rows[i-9]["VendorCode"].ToString().Trim();
myExcel.Cells[i,4]=ds.Tables[0].Rows[i-9]["VendorName"].ToString().Trim();
myExcel.Cells[i,5]="";
myExcel.Cells[i,6]="";
myExcel.Cells[i,7]="";
myExcel.Cells[i,8]=ds.Tables[0].Rows[i-9]["PayType"].ToString().Trim();
myExcel.Cells[i,9]=ds.Tables[0].Rows[i-9]["TermsCode"].ToString().Trim();
myExcel.Cells[i,10]=Convert.ToDateTime(ds.Tables[0].Rows[i-9]["CreateTime"].ToString()).ToShortDateString().Trim();
if(Convert.ToDateTime(ds.Tables[0].Rows[i-9]["CreateTime"].ToString()).Month==Convert.ToDateTime(this.txtDateNow.Text).Month)
{
for(int qq=1;qq<=10;qq++)
{
Range r=(Range)myExcel.Cells[i,qq];
r.Font.Color=Information.RGB(0,255,0);
r=null;
}
}
//设置颜色,否则日期显示成"######"格式。
Range rCol10=(Range)myExcel.Cells[i,10];
rCol10.Font.Color=Information.RGB(0,0,0);
rCol10=null;
//从1开始循环
j++;
i++;
}
myBook.Saved=true;
myBook.SaveAs(DownloadFilePath,missing2,"","",false,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,missing,missing);
//myBook.PrintPreview(0);
//myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing);
myBook.Close(false, null,null);
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myBook = null;
myExcel = null;
GC.Collect();
//Response.Redirect(DownloadFilePath);//下载文件
try
{
BomUploadController BUC=new BomUploadController();
BUC.KillProcess("Excel");
}
//错误信息的处理
catch (BomUploadController.FileException ex)
{
string[] errCase=ex.ErrorString.Split("!".ToCharArray());
string script = "<Script language=javascript>";
if(errCase[1].Trim()=="6")
{
script+="alert('" + errCase[0] + this.lblProcErr.Text.Trim()+ "');";
}
script += "</Script>";
Page.RegisterStartupScript("fileException", script);
}
Session["OVSDownloadFilePath"]=DownloadFilePath;
Session["OVSTempFileName"]=TempFileName;
Response.Write("<Script language=/"javascript/">window.open(/"TempShow.aspx/",/"/",/"/");</Script>");
this.BindWebGridColumn();
}
catch(Exception Ex)
{
throw Ex;
}
/*finally
{
BomUploadController BUC=new BomUploadController();
BUC.KillProcess("Excel");
}*/
}
#endregion
//TempShow.aspx.cs页面文件
public class TempShow : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
try
{
string DownloadFilePath="";
string TempFileName="";
if(Session["OVSDownloadFilePath"]!=null && Session["OVSTempFileName"]!=null)
{
DownloadFilePath=Session["OVSDownloadFilePath"].ToString();
TempFileName=Session["OVSTempFileName"].ToString();
}
else
{
Response.Write("<script>window.close();</script>");
return;
}
System.IO.FileInfo fleInfo = new System.IO.FileInfo(DownloadFilePath);
Response.Clear();
Response.Buffer=true;
Response.Charset = "GB2312";
Response.AddHeader("Content-Disposition","attachment; filename="+TempFileName.Trim()+"");
//'------------------------------------------inline(在线打开),attachment(下载)
Response.AddHeader("Content-Length", fleInfo.Length.ToString());
Response.ContentType = "application/ms-excel";
Response.WriteFile(fleInfo.FullName);
Session.Remove("OVSDownloadFilePath");
Session.Remove("OVSTempFileName");
//Response.Write("<script>window.close();</script>");
//Response.End();
}
catch(Exception Ex)
{
throw Ex;
}
finally
{
//Response.Write("<script>alert('haha');</script>");
Response.Write("<Script>window.close();</Script>");
Response.End();
}
}
}