ISheet ICell

本文介绍如何使用Excel 2003及2007中HSSFWorkbook和XSSFWorkbook获取表格的基本信息,包括首尾行号、首尾单元格号,并提供单元格的字符串表示方法。适用于希望通过API操作Excel文件的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

 /// <summary>
        /// Gets the first row on the sheet
        /// </summary>
        /// <value>the number of the first logical row on the sheet (0-based).</value>
        int FirstRowNum { get; }

        /// <summary>
        /// Gets the last row on the sheet
        /// </summary>
        /// <value>last row contained n this sheet (0-based)</value>
        int LastRowNum { get; }

 

 

/// <summary>
        /// Get the number of the first cell Contained in this row.
        /// </summary>
        /// <returns>
        /// short representing the first logical cell in the row,
        /// or -1 if the row does not contain any cells.
        /// </returns>
        short FirstCellNum { get; }
        
            /// <summary>
        /// Gets the index of the last cell Contained in this row <b>PLUS ONE</b>. The result also
        /// happens to be the 1-based column number of the last cell.  This value can be used as a
        /// standard upper bound when iterating over cells:
        /// <pre>
        /// short minColIx = row.GetFirstCellNum();
        /// short maxColIx = row.GetLastCellNum();
        /// for(short colIx=minColIx; colIx&lt;maxColIx; colIx++) {
        /// Cell cell = row.GetCell(colIx);
        /// if(cell == null) {
        /// continue;
        /// }
        /// //... do something with cell
        /// }
        /// </pre>
        /// </summary>
        /// <returns>
        /// short representing the last logical cell in the row <b>PLUS ONE</b>,
        /// or -1 if the row does not contain any cells.
        /// </returns>
        short LastCellNum { get; }

 

 

 

 

 

 

Excel2003

HSSFWorkbook

D:\ChuckLu\Git\GitHub\Other\npoi\main\HSSF\UserModel\HSSFCell.cs

 /// <summary>
        /// Returns a string representation of the cell
        /// This method returns a simple representation,
        /// anthing more complex should be in user code, with
        /// knowledge of the semantics of the sheet being Processed.
        /// Formula cells return the formula string,
        /// rather than the formula result.
        /// Dates are Displayed in dd-MMM-yyyy format
        /// Errors are Displayed as #ERR&lt;errIdx&gt;
        /// </summary>
        public override String ToString()
        {
            switch (CellType)
            {
                case CellType.Blank:
                    return "";
                case CellType.Boolean:
                    return BooleanCellValue ? "TRUE" : "FALSE";
                case CellType.Error:
                    return NPOI.SS.Formula.Eval.ErrorEval.GetText(((BoolErrRecord)_record).ErrorValue);
                case CellType.Formula:
                    return CellFormula;
                case CellType.Numeric:
                    string format = this.CellStyle.GetDataFormatString();
                    DataFormatter formatter = new DataFormatter();
                    return formatter.FormatCellValue(this);
                case CellType.String:
                    return StringCellValue;
                default:
                    return "Unknown Cell Type: " + CellType;
            }

        }
 

 

 

 

Excel2007

XSSFWorkbook

 

D:\ChuckLu\Git\GitHub\Other\npoi\ooxml\XSSF\UserModel\XSSFCell.cs

 /// <summary>
        /// Returns a string representation of the cell
        /// </summary>
        /// <returns>Formula cells return the formula string, rather than the formula result.
        /// Dates are displayed in dd-MMM-yyyy format
        /// Errors are displayed as #ERR&lt;errIdx&gt;
        /// </returns>
        public override String ToString()
        {
            switch (CellType)
            {
                case CellType.Blank:
                    return "";
                case CellType.Boolean:
                    return BooleanCellValue ? "TRUE" : "FALSE";
                case CellType.Error:
                    return ErrorEval.GetText(ErrorCellValue);
                case CellType.Formula:
                    return CellFormula;
                case CellType.Numeric:
                    if (DateUtil.IsCellDateFormatted(this))
                    {
                        FormatBase sdf = new SimpleDateFormat("dd-MMM-yyyy");
                        return sdf.Format(DateCellValue, CultureInfo.CurrentCulture);
                    }
                    return NumericCellValue.ToString();
                case CellType.String:
                    return RichStringCellValue.ToString();
                default:
                    return "Unknown Cell Type: " + CellType;
            }
        }
 

 

if (saveDataSend.ShowDialog() == DialogResult.OK) // 显示文件框,并且选择文件 { string fName = saveDataSend.FileName; // 获取文件名 // 创建工作簿 IWorkbook workbook = new XSSFWorkbook(); ISheet sheet = workbook.CreateSheet("Sheet1"); // 写入列标题 IRow headerRow = sheet.CreateRow(0); ICell cellOne = headerRow.CreateCell(0); cellOne.SetCellValue("文件名"); ICell cellTwo = headerRow.CreateCell(1); cellTwo.SetCellValue("image"); int hangNum = 0; for (int i = 0; i < fileDialog.Count(); i++) { string[] files = Directory.GetFiles(fileDialog[i]); for (int p = 0; p < files.Count(); p++) { if (!files[p].Contains("_1")) { continue; } if (files[p].Contains(".png") || files[p].Contains(".PNG") || files[p].Contains(".jpeg") || files[p].Contains(".JPEG") || files[p].Contains(".bmp") || files[p].Contains(".BMP")) { hangNum++; IRow dataRow = sheet.CreateRow(hangNum); ICell mingCheng = dataRow.CreateCell(0); mingCheng.SetCellValue(Path.GetFileName(files[p])); // 如果单元格包含图片 Bitmap bitmap = new Bitmap(files[p]); using (MemoryStream ms = new MemoryStream()) { bitmap.Save(ms, ImageFormat.Jpeg); byte[] bytes = ms.ToArray(); int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG); XSSFDrawing drawing = (XSSFDrawing)sheet.CreateDrawingPatriarch(); // 计算图片的大小 double imageWidth = bitmap.Width * 0.075; // 将像素转换为Excel的单位 double imageHeight = bitmap.Height * 0.075; // 将像素转换为Excel的单位 // 设置列宽和行高 sheet.SetColumnWidth(1, (short)(imageWidth * 427)); // 列宽单位是1/256个字符宽 sheet.DefaultRowHeight = (short)(imageHeight * 20); // 行高单位是1/20个点 XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 1, hangNum, 2, hangNum + 1); drawing.CreatePicture(anchor, pictureIdx); } } // 保存Excel文件 using (FileStream fs = new FileStream(fName, FileMode.OpenOrCreate, FileAccess.Write)) { workbook.Write(fs); } } } MessageBox.Show("数据导出成功!"); } 就是这段写入图片的代码,图片多的情况下会报内存不足,怎样优化呢
05-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值