Beispiel #1
0
        /// <summary>
        /// 从第1行填值
        /// </summary>
        /// <param name="sheet"></param>
        private void SetValueByRow(ref ISheet sheet, TrayInfoInExcel trayInfoInExcel, ICellStyle style)
        {
            int  nextRowNum = sheet.LastRowNum + 1;
            IRow sheetRow   = sheet.CreateRow(nextRowNum);

            int   column = 0;
            ICell cell   = sheetRow.CreateCell(column);             //创建Excel的ICell单元格实例

            cell.SetCellValue(nextRowNum);
            cell.CellStyle = style;                                 //单元格格式

            column++;
            cell = sheetRow.CreateCell(column);                     //创建Excel的ICell单元格实例
            cell.SetCellValue(trayInfoInExcel.sn);                  //赋值 sn
            cell.CellStyle = style;                                 //单元格格式

            column++;
            cell = sheetRow.CreateCell(column);                     //创建Excel的ICell单元格实例
            cell.SetCellValue(trayInfoInExcel.imei);                //赋值 imei
            cell.CellStyle = style;                                 //单元格格式

            column++;
            cell = sheetRow.CreateCell(column);                     //创建Excel的ICell单元格实例
            //cell.SetCellValue(casingInfoList[i].station);           //赋值 eid
            cell.SetCellValue(trayInfoInExcel.eid);
            cell.CellStyle = style;                                 //单元格格式

            column++;
            cell = sheetRow.CreateCell(column);                     //创建Excel的ICell单元格实例
            cell.SetCellValue(trayInfoInExcel.iccid);               //赋值 iccid
            cell.CellStyle = style;                                 //单元格格式

            column++;
            cell = sheetRow.CreateCell(column);                     //创建Excel的ICell单元格实例
            cell.SetCellValue(trayInfoInExcel.cuatomerName);
            //cell.SetCellValue(casingInfoList[i].factory);         //赋值 客户名称
            cell.CellStyle = style;                                 //单元格格式
        }
Beispiel #2
0
        public int ExportExcelOneByOne(TrayInfoInExcel trayInfoInExcel)
        {
            int ret = -1;

            //获取excel文件信息
            FileInfo file = new FileInfo(excelFullName);

            //检测文件是否存在
            if (!File.Exists(excelFullName))
            {
                if (CreatExcel() != 0)
                {
                    return(ret);
                }
            }
            else
            {
                file.IsReadOnly = false;
            }


            HSSFWorkbook workbook = null;

            try
            {
                using (FileStream fs1 = File.OpenRead(excelFullName))
                {
                    workbook = new HSSFWorkbook(fs1);                         //实例化Excel工作薄类
                    ISheet sheet = workbook.GetSheet("Sheet1");

                    ICellStyle cellstyle = workbook.CreateCellStyle();                  //设置垂直居中
                    cellstyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
                    //SetValueByRow中自动设序号 +1
                    //int rowCount = sheet.LastRowNum;
                    //trayInfoInExcel.num = rowCount;
                    //设定值
                    SetValueByRow(ref sheet, trayInfoInExcel, cellstyle);
                    SetSheetAutoSize(ref sheet);
                    fs1.Close();

                    using (FileStream fs2 = File.OpenWrite(excelFullName))
                    {
                        workbook.Write(fs2);
                        workbook.Close();
                    }
                    ret = 0;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (workbook != null)
                {
                    workbook.Close();
                }
                file.IsReadOnly = true;
            }

            return(ret);
        }