首页 新闻 会员 周边 捐助

npoi导出将单元格设置为自动换行

0
悬赏园豆:20 [已解决问题] 解决于 2018-09-09 21:33

导出的某个单元格只有4个中文字符的宽度,现在导出6个中文字符,要将这个单元格在C#后台设置自动换行。同excel 上的设置单元格格式-对齐-自动换行。

多多多多多多  多多多多

                   多多

我下面写的  styleTitle.WrapText = true;是不是还要其他的什么处理才能换行效果(上面6个多的效果)

 

#region 导出数据
        /// <summary>
        /// 生成交换机HSSFWorkbook数据源
        /// </summary>
        /// <param name="sSheetName"></param>
        /// <param name="sAddress"></param>
        /// <param name="sOrganizationID"></param>
        /// <returns></returns>
        private HSSFWorkbook BuildSwitchDataScheduler(string sSheetName, string sAddress, string sOrganizationID, string MaterialRequisitionNo)
        {
            try
            {
                HSSFWorkbook wb = new HSSFWorkbook();
                HSSFSheet sheet = (HSSFSheet)wb.CreateSheet(sSheetName); //创建工作表
                sheet.CreateFreezePane(0, 1); //冻结列头行
                HSSFRow row_Title = (HSSFRow)sheet.CreateRow(0); //创建列头行
                row_Title.HeightInPoints = 24F; //设置列头行高
                ICell cellTitle = row_Title.CreateCell(0);

                ICellStyle styleTitle = wb.CreateCellStyle();
                styleTitle.WrapText = true;//自动换行
                styleTitle.Alignment = HorizontalAlignment.Center;
                styleTitle.VerticalAlignment = VerticalAlignment.Center;
                IFont fontTitle = wb.CreateFont();
                fontTitle.FontName = "宋体";
                fontTitle.FontHeightInPoints = 9;
                styleTitle.SetFont(fontTitle);
           

                #region 设置列宽
                sheet.SetColumnWidth(0, 24 * 120);
                sheet.SetColumnWidth(1, 24 * 120);
                sheet.SetColumnWidth(2, 24 * 50);
                sheet.SetColumnWidth(3, 24 * 80);
                sheet.SetColumnWidth(4, 24 * 80);
                sheet.SetColumnWidth(5, 24 * 50);
                sheet.SetColumnWidth(6, 24 * 100);
                sheet.SetColumnWidth(7, 24 * 100);
                sheet.SetColumnWidth(8, 24 * 80);
                sheet.SetColumnWidth(9, 24 * 80);
                sheet.SetColumnWidth(10, 24 * 80);
                sheet.SetColumnWidth(11, 24 * 80);
                sheet.SetColumnWidth(12, 24 * 80);
                sheet.SetColumnWidth(13, 24 * 80);
                sheet.SetColumnWidth(14, 24 * 80);
                sheet.SetColumnWidth(15, 24 * 80);
                sheet.SetColumnWidth(16, 24 * 80);
                sheet.SetColumnWidth(17, 24 * 80);
                sheet.SetColumnWidth(18, 24 * 80);
                sheet.SetColumnWidth(19, 24 * 80);
                sheet.SetColumnWidth(20, 24 * 80);
                sheet.SetColumnWidth(21, 24 * 80);
                sheet.SetColumnWidth(22, 24 * 80);
                //sheet.SetColumnWidth(23, 20 * 250);
                //sheet.SetColumnWidth(24, 20 * 250);
                #endregion

                #region 设置列头单元格样式                
                HSSFCellStyle cs_Title = (HSSFCellStyle)wb.CreateCellStyle(); //创建列头样式
                cs_Title.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; //水平居中
                cs_Title.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; //垂直居中
                HSSFFont cs_Title_Font = (HSSFFont)wb.CreateFont(); //创建字体
                cs_Title_Font.IsBold = true; //字体加粗
                cs_Title_Font.FontHeightInPoints = 9; //字体大小
                cs_Title.SetFont(cs_Title_Font); //将字体绑定到样式  
                #endregion

                #region 生成列头
                IRow rowHead = sheet.CreateRow(0);
                for (int i = 0; i < 24; i++)
                {
                    HSSFCell cell_Title = (HSSFCell)rowHead.CreateCell(i); //创建单元格
                    cell_Title.CellStyle = cs_Title; //将样式绑定到单元格
                    switch (i)
                    {
                        case 0:
                            cell_Title.SetCellValue("机器");
                            break;
                        ......
                        case 22:
                            cell_Title.SetCellValue("数量");
                            break;

                    }
                }
                #endregion
                double count = 0;
                double exportnum = 0;
                double exportweight = 0;
                int iCount = 0;
                var Do = SchedulerRule.GetSchedulerVWByCondition(string.Format("MaterialRequisitionNo='{0}'", MaterialRequisitionNo), "");
                //List<SwitchModel> lS = ExportDataHandler.BuildSwitchDataList(out iCount, null, null, sAddress, sOrganizationID); //项目中的数据源生成方法,此示例中请忽略
                if (Do != null)
                {

                    for (int i = 0; i < Do.Count; i++)
                    {
                        #region 设置内容单元格样式
                        HSSFCellStyle cs_Content = (HSSFCellStyle)wb.CreateCellStyle(); //创建列头样式
                        cs_Content.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; //水平居中
                        cs_Content.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; //垂直居中
                        #endregion
                        //count += Do[i].ProductTotalPrice;
                        //exportnum += Do[i].ProductNum;
                        //exportweight += Do[i].Weight;
                        #region 生成内容单元格
                        HSSFRow row_Content = (HSSFRow)sheet.CreateRow(i + 1); //创建行
                        row_Content.HeightInPoints = 24;
                        cs_Content.SetFont(cs_Title_Font); //将字体绑定到样式  
                        for (int j = 0; j < 27; j++)
                        {
                            HSSFCell cell_Conent = (HSSFCell)row_Content.CreateCell(j); //创建单元格
                            cell_Conent.CellStyle = cs_Content;
                            switch (j)
                            {
                                case 0:
                                    cell_Conent.SetCellValue(Do[i].DeviceNo);
                                    break;
                                case 1:
                                    cell_Conent.SetCellValue(Do[i].OrderNo);
                                    break;
                                case 2:
                                    cell_Conent.SetCellValue(Do[i].OrderTime.ToString("MM-dd"));
                                    break;
                                case 3:
                                    cell_Conent.SetCellValue(Do[i].HeadCustomerName);

                                    break;
                                case 4:
                                    cell_Conent.SetCellValue(Do[i].CustName);
                                    break;
                                case 5:
                                    cell_Conent.SetCellValue(Do[i].ExpirationTime.ToString("MM-dd"));
                                    break;
                                case 6:
                                    cell_Conent.SetCellValue(Do[i].MainOrderNo);
                                    break;
                                case 7:
                                    cell_Conent.SetCellValue(Do[i].DetailOrderNo);
                                    break;
                                case 8:
                                    cell_Conent.SetCellValue(Do[i].CustomerPO);
                                    break;
                                case 9:
                                    cell_Conent.SetCellValue(Do[i].FilaPO);
                                    break;
                                case 10:
                                    cell_Conent.SetCellValue(Do[i].StyleNo);
                                    break;
                                case 11:
                                    cell_Conent.SetCellValue(Do[i].ShoesName);
                                    break;
                                case 12:
                                    cell_Conent.SetCellValue(Do[i].ProductNo);
                                    break;
                                case 13:
                                    cell_Conent.SetCellValue(Do[i].ProductName);
                                    break;
                                case 14:
                                    cell_Conent.SetCellValue(Do[i].ProductSize);
                                    break;
                                case 15:
                                    cell_Conent.SetCellValue(Do[i].OrderNum);
                                    break;
                                case 16:
                                    cell_Conent.SetCellValue(Do[i].OrderMaterialNo);
                                    break;
                                case 17:
                                    cell_Conent.SetCellValue(Do[i].PaperMaterial);
                                    break;
                                case 18:
                                    cell_Conent.SetCellValue(Do[i].NormalImpression);
                                    break;
                                case 19:
                                    cell_Conent.SetCellValue(Do[i].PaperCost);
                                    break;
                                case 20:
                                    cell_Conent.SetCellValue(Do[i].PaperMaterialShoeNo);
                                    break;
                                case 21:
                                    cell_Conent.SetCellValue(Do[i].PaperMeter);
                                    break;
                                //case 22:
                                //    cell_Conent.SetCellValue(Do[i].PaperMaterialLong);
                                //    break;
                                //case 23:
                                //    cell_Conent.SetCellValue(Do[i].PaperMaterialWidth);
                                //    break;
                                case 22:
                                    cell_Conent.SetCellValue(Do[i].PaperSum);
                                  
                                    break;
                            }
                        }
                        #endregion
                    }
                }
                //设置自适应列宽
                //for (int i = 0; i < 24; i++)
                //    sheet.AutoSizeColumn(i);
                return wb;
                //HSSFRow row_Title4 = (HSSFRow)sheet.CreateRow(lS.Count + 3); //创建列头行
                //row_Title4.CreateCell(1).SetCellValue("合计数量");
                //row_Title4.CreateCell(2).SetCellValue(exportnum);
                //row_Title4.CreateCell(3).SetCellValue("合计重量");
                //row_Title4.CreateCell(4).SetCellValue(exportweight.ToString("F3"));
                //row_Title4.CreateCell(5).SetCellValue("合计金额");
                //row_Title4.CreateCell(6).SetCellValue(count.ToString("F2"));
            }
            catch { }
            return null;
        }
        public JsonResult SchedulerExport()
        {
            var jsonReturnMsg = new JsonReturnMsg();
            string sExportFileName = ""; //导出的临时文件的名称
            string sExportFilePath = ""; //导出的临时文件路径
            try
            {
                HSSFWorkbook wb = null;
                string sFileName = "排单导出列表";
                //string CustName = TypeHelper.ToString(Request["CustName"]);
                //DateTime StartDT = TypeHelper.ToDateTime(Request["StartDT"]);
                //DateTime EndDT = TypeHelper.ToDateTime(Request["EndDT"]);
                string MaterialRequisitionNo = TypeHelper.ToString(Request["MaterialRequisitionNo"]);
                wb = BuildSwitchDataScheduler(sFileName, "", "", MaterialRequisitionNo);

                string sWebBasePath = HttpContext.Server.MapPath("~"); //获取网站根目录物理路径
                string sExportDir = sWebBasePath + "/Export"; //临时保存文件夹
                sExportFileName = sFileName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                sExportFilePath = sExportDir + "/" + sExportFileName;
                if (!Directory.Exists(sExportDir))
                    Directory.CreateDirectory(sExportDir);

                using (FileStream file = new FileStream(sExportFilePath, FileMode.Create))
                {
                    wb.Write(file);
                }
                string filePath = "/Export/" + sExportFileName;
                jsonReturnMsg.IsSuccess = true;
                jsonReturnMsg.Data = new { FilePath = filePath };
            }
            catch
            {
                jsonReturnMsg.Msg = "导出失败请重试。";
                jsonReturnMsg.IsSuccess = false;
            }
            return Json(jsonReturnMsg, JsonRequestBehavior.AllowGet);
        }
        #endregion
大神带带我吧的主页 大神带带我吧 | 初学一级 | 园豆:25
提问于:2018-08-06 16:29
< >
分享
最佳答案
0

我都是预置一个模板,里面的格式都自己调好,然后往里写数据,并不是什么都是代码来完成

收获园豆:16
猝不及防 | 老鸟四级 |园豆:2878 | 2018-08-07 09:23
其他回答(2)
0

说个题外话,你把数据都写进去,拿表格打开一全选点下居中什么的就完事了,何必写这么多代码,让使用者自己调一下呗

收获园豆:2
DanBrown | 园豆:1321 (小虾三级) | 2018-08-06 16:45
0
HSSFCellStyle cs_Content = (HSSFCellStyle)wb.CreateCellStyle(); //创建列头样式
cs_Content.Alignment =NPOI.SS.UserModel.HorizontalAlignment.Center; //水平居中
cs_Content.VerticalAlignment =NPOI.SS.UserModel.VerticalAlignment.Center; //垂直居中

cs_Content.WrapText  = true;

 

这个ICellStyle看名字就知道是对单元格的样式的设置,你没有对单元格进行设置。

加行代码设置下

收获园豆:2
禾勿 | 园豆:222 (菜鸟二级) | 2018-08-06 17:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册