很多时候我们需要对数据进行一些特殊处理 ,前不久我做OA管理系统时遇到一个问题 ,需要将表打印出来
那么首先要打印就进行特殊处理,比如我是将它转成excel文档 。
NOPI确实是个比较好用的东西 ,
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 < 10; i++)
{
IRow row1 = sheet1.CreateRow(i);
IRow row2 = sheet2.CreateRow(i);
IRow row3 = sheet3.CreateRow(i);
for (int j = 0; j < 10; 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");
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 < 10; i++)
{
IRow row1 = sheet1.CreateRow(i);
IRow row2 = sheet2.CreateRow(i);
IRow row3 = sheet3.CreateRow(i);
for (int j = 0; j < 10; 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");
引用
Aspose.Cells.dll
后 ,直接在控制器里写出代码,当然这也是在百度可以找到的 ,改成自己想要的就行啦
public ActionResult daochu()
{
//读取数据
UserDBEntities context = new UserDBEntities();
var result = context.UserInfo.Where(u=>true);
//读取模板
Workbook book = new Workbook();
book.Open(Server.MapPath("/Test/User.xlsx"));
//获取sheet
var sheet=book.Worksheets[0];
//获取单元格
var cell = sheet.Cells;
int row = 3;
//插入数据
foreach (var item in result)
{
cell[row, 0].PutValue(item.id);
cell[row, 1].PutValue(item.name);
cell[row, 2].PutValue(item.age);
row++;
}
//保存
string fileName = Guid.NewGuid().ToString() + ".xlsx";
book.Save(Server.MapPath("/Test")+"\"+fileName);
//下载
return File(Server.MapPath("/Test") + "\" + fileName,"application/book","Users.xlsx");
}
}
}
{
//读取数据
UserDBEntities context = new UserDBEntities();
var result = context.UserInfo.Where(u=>true);
//读取模板
Workbook book = new Workbook();
book.Open(Server.MapPath("/Test/User.xlsx"));
//获取sheet
var sheet=book.Worksheets[0];
//获取单元格
var cell = sheet.Cells;
int row = 3;
//插入数据
foreach (var item in result)
{
cell[row, 0].PutValue(item.id);
cell[row, 1].PutValue(item.name);
cell[row, 2].PutValue(item.age);
row++;
}
//保存
string fileName = Guid.NewGuid().ToString() + ".xlsx";
book.Save(Server.MapPath("/Test")+"\"+fileName);
//下载
return File(Server.MapPath("/Test") + "\" + fileName,"application/book","Users.xlsx");
}
}
}
09网络 来围观大神杰作啊