public IActionResult CreateWord()
        {
            var fileName = _context.DataFile
                           .Select(i => i.FileName)
                           .First();

            var previousExtension = fileName.Split('.')[1];

            var wordFile = fileName.Replace(previousExtension, "docx");

            var targetFileDirectory = Path.Combine(_hostingEnvironment.ContentRootPath, "Uploads");

            if (!Directory.Exists(targetFileDirectory))
            {
                Directory.CreateDirectory(targetFileDirectory);
            }

            var filePath = Path.Combine(targetFileDirectory, wordFile);

            WordHelper.CreateWordprocessingDocument(filePath);

            var run = new RunFonts
            {
                Ascii = "Calibri"
            };
            var wp = new WordHelper("14.1%", "26.7%", "59.1%", "24", run);

            var lineItems = (from tempData in _context.TempData
                             join category in _context.Category on tempData.Category equals category.Name
                             join lineItem in _context.LineItem on tempData.Name equals lineItem.Name
                             join mapping in _context.Mapping on new { CategoryId = category.Id, LineItemId = lineItem.Id } equals new { mapping.CategoryId, mapping.LineItemId }
                             orderby mapping.LineItemId, lineItem.LineNumber
                             select new
            {
                Category = category.Name,
                mapping.LineItem.Name,
                mapping.Description,
                mapping.LineItem.LineNumber,
                mapping.Id
            })
                            .ToList();

            var model = (from lineItem in lineItems.GroupBy(i => new { i.Category, i.Description })
                         let lineNumbers = lineItem.Select(i => i.LineNumber)
                                           let lineNames = lineItem.Select(i => i.Name)
                                                           select new LineItemViewModel
            {
                Category = lineItem.Key.Category,
                Description = lineItem.Key.Description,
                WordLineNumbers = string.Join(',', lineNumbers),
                Name = string.Join(Environment.NewLine + Environment.NewLine, lineNames)
            }).ToList();

            wp.AddTable(filePath, model);

            var fileBytes = System.IO.File.ReadAllBytes(filePath);

            return(File(fileBytes, MimeTypeHelper.GetContentType(filePath), wordFile));
        }
Пример #2
0
        public static void 添加表格()
        {
            List <Student> students = new List <Student>()
            {
                new Student()
                {
                    StuNo = "111111", Age = 10, PhoneNum = "4321", Nationality = "Japan", Sex = 5
                },
                new Student()
                {
                    StuNo = "222222", Age = 20, PhoneNum = "1243", Nationality = "Japan", Sex = 4
                },
                new Student()
                {
                    StuNo = "333333", Age = 30, PhoneNum = "4321", Nationality = "Japan", Sex = 3
                },
                new Student()
                {
                    StuNo = "444444", Age = 10, PhoneNum = "1234", Nationality = "Japan", Sex = 2
                },
            };

            DataTable td = ListDatatableMapper <Student> .ListToDataTable(students);

            XWPFDocument doc = new XWPFDocument();
            Func <XWPFDocument, XWPFTable, string, XWPFParagraph> ff = (x, table, setText) =>
            {
                NPOI.OpenXmlFormats.Wordprocessing.CT_P para = new NPOI.OpenXmlFormats.Wordprocessing.CT_P();
                XWPFParagraph pCell = new XWPFParagraph(para, table.Body);
                pCell.Alignment         = ParagraphAlignment.CENTER; //字体居中
                pCell.VerticalAlignment = TextAlignment.CENTER;      //字体居中

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

            WordHelper.AddTable(doc, td, ff);

            FileStream ms = new FileStream(docToPath, FileMode.OpenOrCreate, FileAccess.Write);

            doc.Write(ms);
            ms.Dispose();
        }
Пример #3
0
        public static void 已有的文档添加表格_添加文字_添加表格()
        {
            XWPFDocument doc = null;

            // 加载文件
            using (FileStream fs = new FileStream(Demo.docPath, FileMode.Open, FileAccess.Read))
            {
                doc = new XWPFDocument(fs);
            }
            // 添加表格
            List <Student> stus = new List <Student>()
            {
                new Student()
                {
                    StuNo = "111111", Age = 10, PhoneNum = "4321", Nationality = "Japan", Sex = 5
                },
                new Student()
                {
                    StuNo = "222222", Age = 20, PhoneNum = "1243", Nationality = "Japan", Sex = 4
                },
                new Student()
                {
                    StuNo = "333333", Age = 30, PhoneNum = "4321", Nationality = "Japan", Sex = 3
                },
                new Student()
                {
                    StuNo = "444444", Age = 10, PhoneNum = "1234", Nationality = "Japan", Sex = 2
                },
            };
            DataTable dt = ListDatatableMapper <Student> .ListToDataTable(stus);

            WordHelper.AddTable(doc, dt);

            // 添加文字
            WordHelper.AddParagrath(doc, "中间文字");

            // 添加表格
            List <Room> rooms = new List <Room>()
            {
                new Room()
                {
                    ID = 1, Name = "Room1", Floor = "1"
                },
                new Room()
                {
                    ID = 2, Name = "Room2", Floor = "3"
                },
                new Room()
                {
                    ID = 3, Name = "Room3", Floor = "2"
                },
            };
            DataTable dt2 = ListDatatableMapper <Room> .ListToDataTable(rooms);

            WordHelper.AddTable(doc, dt2);
            // 保存

            using (FileStream fs = new FileStream(docToPath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                doc.Write(fs);
            }
        }