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();
            }
        }
    }

  • 相关阅读:
    HTML5简介
    PHP
    纯CSS3写的10个不同的酷炫图片遮罩层效果
    零基础如何自学MySQL数据库?
    js与jQuery
    MAC下GitHub命令操作
    框架基础:ajax设计方案(二)---集成轮询技术
    框架基础:ajax设计方案(一)---集成核心请求
    框架基础:ajax设计方案(三)---集成ajax上传技术
    Jquery操作下拉列表和复选框,自定义下拉
  • 原文地址:https://www.cnblogs.com/yuhanzhong/p/2563331.html
Copyright © 2011-2022 走看看