导出execle

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.HSSF.Util;

public partial class ExportExcel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //导出过后的文件名
        string filename = "test.xls";
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));
        Response.Clear();
        switch (this.Request["ExpType"])
        {
//没有模板的时候
            case "NoTemplate":
                InitializeWorkbook(null);
                string stu= this.Request["stu"];
                GenerateData(stu);
                break;
//有模板的时候
            case "Template":
                string path = Server.MapPath("template/Stu.xls");
                InitializeWorkbook(path);
                ExportToTemplate();
                break;
        }
        Response.BinaryWrite(WriteToStream().GetBuffer());
        Response.End();
    }
    static HSSFWorkbook hssfworkbook;

    MemoryStream WriteToStream()
    {
        //Write the stream data of workbook to the root directory
        MemoryStream file = new MemoryStream();
        hssfworkbook.Write(file);
        return file;
    }

    private void GenerateData(string studentCode)
    {
        //写标题文本

        HSSFSheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
        HSSFCell cellTitle = sheet1.CreateRow(0).CreateCell(0);
        cellTitle.SetCellValue("XXXXXXX");
        //设置标题行样式

        HSSFCellStyle style = hssfworkbook.CreateCellStyle();
        //style.Alignment = HSSFCellStyle.ALIGN_CENTER;
        HSSFFont font = hssfworkbook.CreateFont();
        font.FontHeight = 20 * 20;
        style.SetFont(font);
        //style.BorderBottom = HSSFCellStyle.BORDER_THICK;
        //style.BorderLeft = HSSFCellStyle.BORDER_THICK;
        //style.BorderRight = HSSFCellStyle.BORDER_THICK;
        //style.BorderTop = HSSFCellStyle.BORDER_THICK;
        cellTitle.CellStyle = style;
        //合并标题行

        sheet1.AddMergedRegion(new Region(0, 0, 1, 4));
        StudentRepository Repository = new StudentRepository();
        List<StudentMasterPlan> lStudentMasterPlan = Repository.GetStudent(studentCode).VIPExtention.StudentMasterPlans.ToList();
        HSSFRow row;
        HSSFCell cell;
        //HSSFCellStyle celStyle = getCellStyle();
        HSSFPatriarch patriarch = sheet1.CreateDrawingPatriarch();
        //HSSFClientAnchor anchor;
        //HSSFSimpleShape line;
        int rowIndex;
        row = sheet1.CreateRow(2);
        cell = row.CreateCell(0);
        cell.SetCellValue("学期");
        //cell.CellStyle = celStyle;
        cell = row.CreateCell(1);
        cell.SetCellValue("学习课程");
        //cell.CellStyle = celStyle;
        cell = row.CreateCell(2);
        cell.SetCellValue("参加考试");
        //cell.CellStyle = celStyle;
        cell = row.CreateCell(3);
        cell.SetCellValue("建议活动");
        //cell.CellStyle = celStyle;
        cell = row.CreateCell(4);
        cell.SetCellValue("词汇量");
        //cell.CellStyle = celStyle;
        for (int i = 0; i < lStudentMasterPlan.Count; i++)
        {
            //表头数据
            rowIndex = i + 2;
            row = sheet1.CreateRow(rowIndex);
            //设置值和计算公式
            row = sheet1.CreateRow(rowIndex + 1);
            cell = row.CreateCell(0);
            cell.SetCellValue(lStudentMasterPlan[i].CodeTermType.Name);
            //cell.CellStyle = celStyle;
            cell = row.CreateCell(1);
            cell.SetCellValue(lStudentMasterPlan[i].StudentClass);
            //cell.CellStyle = celStyle;
            cell = row.CreateCell(2);
            cell.SetCellValue(lStudentMasterPlan[i].JoinTest);
            //cell.CellStyle = celStyle;
            cell = row.CreateCell(3);
            cell.SetCellValue(lStudentMasterPlan[i].SuggestSport);
            //cell.CellStyle = celStyle;
            cell = row.CreateCell(4);
            cell.SetCellValue(lStudentMasterPlan[i].WordCount);
            //cell.CellStyle = celStyle;
        }
    }
    private void ExportToTemplate()
    {
        StudentExtentionRepository extentionrepository = new StudentExtentionRepository();
        StudentPlanTime planTime = new StudentPlanTime();
        planTime = extentionrepository.GetStudentPlanTimeByID(DataHelper.GetInteger(this.Request["plantimeID"]));
        Teacher  teacher = new Teacher  ();
        StudentRepository repository = new StudentRepository();
        HSSFSheet sheet1 = hssfworkbook.GetSheet("Sheet1");
        //create cell on rows, since rows do already exist,it's not necessary to create rows again.
        sheet1.GetRow(2).GetCell(1).SetCellValue(repository.GetStudent(planTime.StudentCode).Name);
        sheet1.GetRow(3).GetCell(1).SetCellValue(planTime.GradeCode.Name);
        sheet1.GetRow(4).GetCell(1).SetCellValue(planTime.PlanArea + "至" + planTime.PlanAreaTo);
        sheet1.GetRow(5).GetCell(1).SetCellValue(teacherRepository.AppendTeacherEntity().GetTeacherEntity(planTime.PlanTeacher).Name);
        sheet1.GetRow(6).GetCell(1).SetCellValue(teacherRepository.AppendTeacherEntity().GetTeacherEntity(planTime.SupervisorTeacher).Name);
        sheet1.GetRow(7).GetCell(1).SetCellValue(planTime.ProjectType);
        sheet1.GetRow(8).GetCell(1).SetCellValue(planTime.ClassCode);
        sheet1.GetRow(9).GetCell(1).SetCellValue(planTime.ExamArrange);
        sheet1.GetRow(3).GetCell(4).SetCellValue(planTime.Summery);
        sheet1.GetRow(11).GetCell(0).SetCellValue(planTime.ClassCode);
        sheet1.GetRow(13).GetCell(0).SetCellValue(planTime.StudyPlan);
        sheet1.GetRow(15).GetCell(0).SetCellValue(planTime.TeacherSubject);

        //Force excel to recalculate all the formula while open
        sheet1.ForceFormulaRecalculation = true;
    }
    private void InitializeWorkbook(string path)
    {
        switch (this.Request["ExpType"])
        {
            case "NoTemplate":
                hssfworkbook = new HSSFWorkbook();
                break;
            case "Template":
                FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
                hssfworkbook = new HSSFWorkbook(file);
                break;
        }
        ////create a entry of DocumentSummaryInformation
        DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
        dsi.Company = "NPOI Team";
        hssfworkbook.DocumentSummaryInformation = dsi;

        ////create a entry of SummaryInformation
        SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
        si.Subject = "NPOI SDK Example";
        hssfworkbook.SummaryInformation = si;
    }
}

你可能感兴趣的:(UI,Web,活动,Excel,LINQ)