zoukankan      html  css  js  c++  java
  • Excel导出实例(打开模板,写信息后另存为新excel文件)

        Microsoft.Office.Interop.Excel.Application excelApp = null;
                Microsoft.Office.Interop.Excel.Workbook workBook;
                Microsoft.Office.Interop.Excel.Worksheet ws = null;
                try
                {
                    excelApp = new Microsoft.Office.Interop.Excel.Application();
                    string url = @"F:\Customs\Excharge\报文计费工具\PtsCost\" + payPackage + ".xls";
                    workBook = excelApp.Workbooks.Open(url,
                        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                        Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                    ws = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Worksheets[1];
                    ws.Cells[7, 9] = serialNo;
                    ws.Cells[14, 4] = tradeName;
                    DateTime date = DateTime.Parse(yyyymm.Substring(0,4)+"-"+yyyymm.Substring(4,2) + "-01");
                    ws.Cells[16, 4] = date.ToString("yyyy-MM-dd");
                    ws.Cells[48, 8] = ws.Cells[16, 7] = date.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
                    if (dicEmlDetail.ContainsKey(Common.EML213))
                    {
                        ws.Cells[21, 8] = dicEmlDetail[Common.EML213];
                    }
                    else
                    {
                        ws.Cells[21, 8] = 0;
                    }
                    if (dicEmlDetail.ContainsKey(Common.EML221))
                    {
                        ws.Cells[22, 8] = dicEmlDetail[Common.EML221];
                    }
                    else
                    {
                        ws.Cells[22, 8] = 0;
                    }
                    if (dicEmlDetail.ContainsKey(Common.EML223))
                    {
                        ws.Cells[23, 8] = dicEmlDetail[Common.EML223];
                    }
                    else
                    {
                        ws.Cells[23, 8] = 0;
                    }
                    if (dicEmlDetail.ContainsKey(Common.EML311))
                    {
                        ws.Cells[24, 8] = dicEmlDetail[Common.EML311];
                    }
                    else
                    {
                        ws.Cells[24, 8] = 0;
                    }
                    if (dicEmlDetail.ContainsKey(Common.EML321))
                    {
                        ws.Cells[25, 8] = dicEmlDetail[Common.EML321];
                    }
                    else
                    {
                        ws.Cells[25, 8] = 0;
                    }
                    if (dicEmlDetail.ContainsKey(Common.EML411))
                    {
                        ws.Cells[26, 8] = dicEmlDetail[Common.EML411];
                    }
                    else
                    {
                        ws.Cells[26, 8] = 0;
                    }
                    ws.Cells[31, 6] = sumEmlCount;
                    if (payPackage == Common.B套餐)
                    {
                        ws.Cells[33, 6] = sumEmlCount - 20 > 0 ? sumEmlCount - 20 : 0;
                    }
                    else
                    {
                        ws.Cells[32, 6] = sumEmlCount;
                    }
                    url = @"F:\Temp\" + payPackage +tradeName+ DateTime.Now.ToString("yyyyMMddHHmmsss") + ".xls";
                    //ws.SaveAs(url,
                    //    Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                    //    Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                    workBook.Saved = true;
                    workBook.SaveCopyAs(url);
                    workBook.Close();
                    excelApp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
                    ws = null;
                    workBook = null;
                    excelApp = null;
                    foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("Excel"))
                    {
                        if (!p.CloseMainWindow())
                        {
                            p.Kill();
                        }
                    }
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

    以上方法为 读取模板,然后写信息后 另存为 新的 excel(模板内容为发生变化) 

    上述方法可以返回一个另存excel的路径path,

    然后下述方法可以弹出另存为对话框:

    FileInfo fi = new FileInfo(path);//excel路径
                HttpResponse contextResponse = HttpContext.Current.Response;
                contextResponse.Clear();
                contextResponse.Buffer = true;
                contextResponse.Charset = "GB2312"; //设置了类型为中文防止乱码的出现 
                contextResponse.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", list[1] + ".xls")); //定义输出文件和文件名 
                contextResponse.AppendHeader("Content-Length", fi.Length.ToString());
                contextResponse.ContentEncoding = Encoding.UTF8;
                contextResponse.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
                contextResponse.WriteFile(fi.FullName);
                contextResponse.Flush();

                contextResponse.End(); 

  • 相关阅读:
    《代码大全》阅读心得一
    vi列模式
    以指定的概率/机会获取元素
    自用VIM配置
    优雅的Javascript
    关于遮罩层
    CSS3属性BorderRadius详解[圆角]
    CSS3属性boxshadow详解[盒子阴影]
    CSS3文字特效
    Css3 Animation详解
  • 原文地址:https://www.cnblogs.com/hyd309/p/2612118.html
Copyright © 2011-2022 走看看