如何在C#中设置导出Excel表格⾥的单元格宽⾼如何在C#中设置导出Excel表格⾥的单元格宽⾼
广州心理咨询师培训废话少说吧,直奔主题。
先来看⼀下还没有设置样式,直接导出数据时的效果图:
是不是觉得上⾯的图⾥的汉字有点靠的太近了?看得不是很顺眼,因为部分的字体被遮住了。
接下来看看设置了宽度和⾼度的效果图:
来看⼀下是如何实现这种效果的。
public ActionResult ExportToExcel(){
//⾸先查询出你要导出的数据
List<BorrowVo> listBorrow = (from tbBorrow in myModels.PW_Borrowoneandonly
join tbCommodity in myModels.PW_Commodity on tbBorrow.CommodityID equals tbCommodity.CommodityID
join tbWarehou in myModels.PW_Warehou on tbBorrow.WarehouID equals tbWarehou.WarehouID join tbSupplier in myModels.PW_Supplier on tbBorrow.SupplierID equals tbSupplier.SupplierID
join tbUr in myModels.PW_Ur on tbBorrow.UrID equals tbUr.UrID
orderby tbBorrow.BorrowID descending
lect new BorrowVo{
BorrowID = tbBorrow.BorrowID, //借出ID
BorrowDate = tbBorrow.BorrowDate, //业务⽇期
BorrowNum = tbBorrow.BorrowNum, //单据编号
CommodityName = tbCommodity.CommodityName, //借出商品
WarehouName = tbWarehou.WarehouName, //⼊库仓库
SupplierName = tbSupplier.SupplierName, //供应商名称
BorrowPlanDate = tbBorrow.BorrowPlanDate, //预计归还⽇期
UrName = tbUr.UrName //经⼿⼈
字幕翻译}).ToList();
HSSFWorkbook workBook = new HSSFWorkbook(); //创建⼯作簿
//创建⼯作表
grammar
NPOI.SS.UrModel.ISheet BorrowSheet = workBook.CreateSheet("借⼊单列表");
//创建表头⾏
NPOI.SS.UrModel.IRow BorrowRow = BorrowSheet.CreateRow(0);
//设置表头
BorrowRow.CreateCell(0).SetCellValue("业务⽇期");
BorrowRow.CreateCell(1).SetCellValue("单据编号");
BorrowRow.CreateCell(2).SetCellValue("供应商名称");
BorrowRow.CreateCell(3).SetCellValue("借⼊商品");
BorrowRow.CreateCell(4).SetCellValue("预计归还⽇期");
BorrowRow.CreateCell(5).SetCellValue("⼊库仓库");
BorrowRow.CreateCell(6).SetCellValue("经⼿⼈");
//为Excel表格添加数据
wap是什么for (int i = 0; i < listBorrow.Count(); i++){
NPOI.SS.UrModel.IRow rowTemp = BorrowSheet.CreateRow(i + 1); //创建⾏
//添加数据
rowTemp.CreateCell(0).SetCellValue(listBorrow[i].BorrowDate);
rowTemp.CreateCell(1).SetCellValue(listBorrow[i].BorrowNum);small
sonya
rowTemp.CreateCell(2).SetCellValue(listBorrow[i].SupplierName);
rowTemp.CreateCell(3).SetCellValue(listBorrow[i].CommodityName);
rowTemp.CreateCell(4).SetCellValue(listBorrow[i].BorrowPlanDate);
rowTemp.CreateCell(5).SetCellValue(listBorrow[i].WarehouName);hero
rowTemp.CreateCell(6).SetCellValue(listBorrow[i].UrName);
}
//根据内容动态调整excel单元格宽度
江门学校BorrowSheet.SetColumnWidth(0, 20 * 250);
BorrowSheet.SetColumnWidth(0, 20 * 250);
BorrowSheet.SetColumnWidth(1, 20 * 250);
BorrowSheet.SetColumnWidth(2, 20 * 250);
BorrowSheet.SetColumnWidth(3, 20 * 250);
BorrowSheet.SetColumnWidth(4, 20 * 250);
BorrowSheet.SetColumnWidth(5, 20 * 250);
BorrowSheet.SetColumnWidth(6, 20 * 250);
//根据内容动态调整excel单元格⾼度
IRow currentRow = BorrowSheet.GetRow(BorrowSheet.FirstRowNum);
ICell currentCell = currentRow.GetCell(BorrowSheet.FirstRowNum);
int length = Encoding.UTF8.GetBytes(currentCell.ToString()).Length;
currentRow.HeightInPoints = 20 * (length / 60 + 1);
/
/为Excel⽂件命名
string fileName = "借⼊单列表" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".xls"; //转化为内存流
服装库存处理
MemoryStream ExcelStream = new MemoryStream();
//将Excel⽂件写⼊内存流
workBook.Write(ExcelStream);
//Seek(0,Seek.begin) 第⼀个参数表⽰相对位置,第⼆个参数表⽰参照位置
ExcelStream.Seek(0, SeekOrigin.Begin);
//MIME⽂件类型(Multipurpo Internet Mail Extensions)多⽤途互联⽹邮件扩展类型
return File(ExcelStream, "application/vnd.ms-excel", fileName);
}
注:在做导⼊功能之前,要把引⽤⼀个东西,名为 NPOI.dll,否则⽆法完成导出的功能