C# 如何对excel word进行操作

2021-04-03 12:24

阅读:671

 HSSFWorkbook book = new HSSFWorkbook();
            using (FileStream fs = new FileStream(Request.MapPath("/Content"+"\\"+"1.xls"),FileMode.Create,FileAccess.ReadWrite))
            {
                // 新增試算表。 
                ISheet sheet1 = book.CreateSheet("試算表 A");
                ISheet sheet2 = book.CreateSheet("試算表 B");
                ISheet sheet3 = book.CreateSheet("試算表 C");

                //依次创建行和列
                for (int i = 0; i                 {
                    IRow row1 = sheet1.CreateRow(i);
                    IRow row2 = sheet2.CreateRow(i);
                    IRow row3 = sheet3.CreateRow(i);

                    for (int j = 0; j                     {
                        ICell cell1 = row1.CreateCell(j);
                        cell1.SetCellValue("第" + (i + 1) + "行,第" + (j + 1) + "列");
                        ICell cell2 = row2.CreateCell(j);
                        cell2.SetCellValue("第" + (i + 1) + "行,第" + (j + 1) + "列");
                        ICell cell3 = row3.CreateCell(j);
                        cell3.SetCellValue("第" + (i + 1) + "行,第" + (j + 1) + "列");
                    }
                }

                book.Write(fs);
                book.Close();
            }
            return File(Request.MapPath("/Content" + "\\" + "1.xls"), "application/book", "1.xls");


评论


亲,登录后才可以留言!