zoukankan      html  css  js  c++  java
  • 根据模板导出Excel

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    using System.Reflection;
    using Excel = Microsoft.Office.Interop.Excel;

    namespace WebApplication1
    {
        
    public partial class _Default : System.Web.UI.Page
        {
            
    protected void Page_Load(object sender, EventArgs e)
            {

            }

            
    protected void Button1_Click(object sender, EventArgs e)
            {
                
    //生成一个新的文件名用全球唯一标识符 (GUID)来标识
                string newpath = Server.MapPath("."+ @"\Excel\" + Guid.NewGuid() + ".xls";
                
    //调用的模板文件
                FileInfo mode = new FileInfo(Server.MapPath("~/1.xls"));
                Excel.Application app 
    = new Excel.Application();
                
    if (app == null)
                {
                    
    return;
                }
                app.Application.DisplayAlerts 
    = false;
                app.Visible 
    = false;

                
    if (mode.Exists)
                {
                    Excel.Workbook tworkbook;
                    Object missing 
    = System.Reflection.Missing.Value;

                    app.Workbooks.Add(missing);
                    
    //调用模板
                    tworkbook = app.Workbooks.Open(mode.FullName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                    Excel.Worksheet tworksheet 
    = (Excel.Worksheet)tworkbook.Sheets[2];
                    tworksheet.Cells[
    62= "测试";
                    tworksheet.Cells[
    82= "测试";
                    tworksheet.SaveAs(newpath, missing, missing, missing, missing, missing, missing, missing, missing, missing);

                    tworkbook.Close(
    false, mode.FullName, missing);
                    app.Workbooks.Close();
                    app.Quit();
                    
    if (app != null)
                    {
                        
    foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("Excel"))
                        {
                            
    //先判断当前进程是否是excel   
                            if (!p.CloseMainWindow())
                            {
                                p.Kill();
                            }
                        }
                    }
                    tworkbook 
    = null;
                    app 
    = null;
                    
    //强制对所有代进行垃圾回收
                    GC.Collect();
                }
                
    //打开保存对话框
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer 
    = false;
                Response.Charset 
    = "UTF-8";
                Response.ContentType 
    = "application/ms-excel";
                Response.AppendHeader(
    "Content-Disposition""attachment;filename=" + Server.UrlEncode(mode.Name));
                Response.ContentEncoding 
    = System.Text.Encoding.GetEncoding("GB2312");
                Response.AppendHeader(
    "Content-Length", mode.Length.ToString());
                Response.Charset 
    = "";
                
    this.EnableViewState = false;
                Response.WriteFile(newpath);
                
    //删除创建的Excel文件
                
    //FileInfo fileinf = new FileInfo(newpath);
                
    //fileinf.Delete();
                
    //关闭连接
                Response.Flush();
                Response.End();
            }
        }
    }

  • 相关阅读:
    bzoj 1017 魔兽地图DotR
    poj 1322 chocolate
    bzoj 1045 糖果传递
    poj 3067 japan
    timus 1109 Conference(二分图匹配)
    URAL 1205 By the Underground or by Foot?(SPFA)
    URAL 1242 Werewolf(DFS)
    timus 1033 Labyrinth(BFS)
    URAL 1208 Legendary Teams Contest(DFS)
    URAL 1930 Ivan's Car(BFS)
  • 原文地址:https://www.cnblogs.com/yuhanzhong/p/2563331.html
Copyright © 2011-2022 走看看