ASP.NET MVC+NPOI 实现导出Word表格

文章目录

    • 1.下载NPOI
    • 2.编写方法(这里以学籍对象为例)

1.下载NPOI

(1)右击项目->选择
ASP.NET MVC+NPOI 实现导出Word表格_第1张图片
(2)安装NPOI
ASP.NET MVC+NPOI 实现导出Word表格_第2张图片

2.编写方法(这里以学籍对象为例)

说明:有关学籍对象的字段,可查看本人之前的文章:https://blog.csdn.net/weixin_45721343/article/details/103909626

		//导出Word
        public ActionResult ExportWord(int id)
        {
             XJ xj = xjService.FindById(id);

            // 创建流
            MemoryStream ms = new MemoryStream();
            // 创建文档对象
            XWPFDocument doc = new XWPFDocument();

            // 设置页边距
            CT_SectPr sectPr = new CT_SectPr();
            sectPr.pgMar = new CT_PageMar();
            sectPr.pgMar.bottom = "1443";
            sectPr.pgMar.top = "1443";
            sectPr.pgMar.left = 1801;
            sectPr.pgMar.right = 1801;
            doc.Document.body.sectPr = sectPr;

            //创建段落
            XWPFParagraph p1 = doc.CreateParagraph();
            p1.Alignment = ParagraphAlignment.CENTER;
            // 该段落的文字以及样式
            XWPFRun runTitle = p1.CreateRun();
            runTitle.IsBold = true;
            runTitle.SetText("学籍信息");
            runTitle.FontSize = 18;
            runTitle.SetFontFamily("仿宋", FontCharRange.None);// 字体设置

            XWPFParagraph p2 = doc.CreateParagraph();
            // 段落格式居中
            p2.Alignment = ParagraphAlignment.CENTER;
            XWPFRun runTitle2 = p2.CreateRun();
            runTitle2.FontSize = 18;

            //创建表格
            XWPFTable tableContent = doc.CreateTable(3, 4);//4行5列
            tableContent.Width = 1000 * 5;
            tableContent.SetColumnWidth(0, 1000);/* 设置列宽 */
            tableContent.SetColumnWidth(1, 1500);
            tableContent.SetColumnWidth(2, 1500);
            tableContent.SetColumnWidth(3, 1000);

            tableContent.GetRow(0).GetCell(0).SetParagraph(SetCellText(doc, tableContent, "学号"));
            tableContent.GetRow(0).GetCell(1).SetParagraph(SetCellText(doc, tableContent, xj.xh));
            tableContent.GetRow(0).GetCell(2).SetParagraph(SetCellText(doc, tableContent, "姓名"));
            tableContent.GetRow(0).GetCell(3).SetParagraph(SetCellText(doc, tableContent, xj.xm));
            tableContent.GetRow(1).GetCell(0).SetParagraph(SetCellText(doc, tableContent, "学院"));
            tableContent.GetRow(1).GetCell(1).SetParagraph(SetCellText(doc, tableContent, xj.py_xs.xsmc));
            tableContent.GetRow(1).GetCell(2).SetParagraph(SetCellText(doc, tableContent, "专业"));
            tableContent.GetRow(1).GetCell(3).SetParagraph(SetCellText(doc, tableContent, xj.py_zy.zymc));
            tableContent.GetRow(2).GetCell(0).SetParagraph(SetCellText(doc, tableContent, "导师"));
            tableContent.GetRow(2).GetCell(1).SetParagraph(SetCellText(doc, tableContent, xj.py_rkjs.jsxm));
            tableContent.GetRow(2).GetCell(2).SetParagraph(SetCellText(doc, tableContent, "年级"));
            tableContent.GetRow(2).GetCell(3).SetParagraph(SetCellText(doc, tableContent, xj.nj));

            //文件名
            string filename = "学籍信息.doc";
            // 将创建好的文档对象写入流
            doc.Write(ms);
            ms.Flush();
            // 将流内容写入字节数据
            byte[] data = ms.ToArray();
            // 指定文件内容,类型以及文件名并返回
            return File(data, "application/octet-stream", filename);
        }

        //设置字体样式
        public XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText)
        {
            //table中的文字格式设置  
            CT_P para = new CT_P();
            XWPFParagraph pCell = new XWPFParagraph(para, table.Body);
            pCell.Alignment = ParagraphAlignment.CENTER;//字体居中  
            pCell.VerticalAlignment = NPOI.XWPF.UserModel.TextAlignment.CENTER;//字体居中  

            XWPFRun r1c1 = pCell.CreateRun();
            r1c1.SetText(setText);
            r1c1.FontSize = 12;
            r1c1.FontFamily = "华文楷体";
            //r1c1.SetTextPosition(20);//设置高度  
            return pCell;
        }

说明:本人页面调用这个方法时使用的是a标签

你可能感兴趣的:(ASP.NET随笔)