Unity NPOI读写2003

#region Excel2003  
    public static void CreatExcel2003()
    {
        HSSFWorkbook hssfworkbook = new HSSFWorkbook();

        create a entry of DocumentSummaryInformation
        DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
        dsi.Company = "NPOI Team";
        hssfworkbook.DocumentSummaryInformation = dsi;

        create a entry of SummaryInformation
        SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
        si.Subject = "NPOI SDK Example";
        hssfworkbook.SummaryInformation = si;

        //here, we must insert at least one sheet to the workbook. otherwise, Excel will say 'data lost in file'
        //So we insert three sheet just like what Excel does
        hssfworkbook.CreateSheet("Sheet1");

        //Write the stream data of workbook to the root directory
        FileStream file = new FileStream(@"E:/test910.xls", FileMode.Create);
        hssfworkbook.Write(file);
        file.Close();

    }

    /// <summary>
    /// 读取xls格式的excel
    /// </summary>
    /// <param name="path"></param>
    /// <param name="position"></param>
    /// <returns></returns>
    public static string ReadExcel2003(string path, int[] position)
    {
        string text = "";
        DataTable dt = new DataTable();
        FileInfo file = new FileInfo(path);
        if (!file.Exists)
        {
            CreatEmptyExcel2003(path);
        }
        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            HSSFWorkbook hssfworkbook = new HSSFWorkbook(fs);

            ISheet sheet = hssfworkbook.GetSheetAt(0);
            if (position[0] <= sheet.LastRowNum && position[1] <= sheet.GetRow(position[0]).LastCellNum)
            {
                object obj = GetValueTypeForXLS(sheet.GetRow(position[0]).GetCell(position[1]) as HSSFCell);
                text = obj.ToString();
            }
            else
            {
                text = string.Empty;
            }
        }
        return text;
    }

    public static void WirteExcel2003(string text, string path, int[] position, string sheetName = "sheet1")
    {

        FileInfo file = new FileInfo(path);
        if (!file.Exists)
        {
            CreatEmptyExcel2003(path);
        }

        FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        HSSFWorkbook hssfworkbook = new HSSFWorkbook(fs);
        
        ISheet sheet = hssfworkbook.GetSheet(sheetName);
        if (sheet == null)
        {
            sheet = hssfworkbook.CreateSheet(sheetName);
        }
        IRow row = sheet.GetRow(position[0]);
        if (row == null)
        {
            row = sheet.CreateRow(position[0]);
        }

        ICell cell = row.GetCell(position[1]);
        if (cell == null)
        {
            cell = row.CreateCell(position[1]);
        }


        cell.SetCellValue(text);

        MemoryStream stream = new MemoryStream();
        hssfworkbook.Write(stream);
        var buf = stream.ToArray();
        fs.Close();

        fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
        fs.Write(buf, 0, buf.Length);
        fs.Flush();
        fs.Close();
        
    }

    /// <summary>
    /// 写Excel
    /// </summary>
    /// <param name="text"></param>
    /// <param name="path"></param>
    /// <param name="position"></param>
    /// <param name="sheetName"></param>
    public static void WirtesExcel2003(string[] text, string path, int[][] position, string sheetName = "sheet1")
    {
        //文件存在检测
        FileInfo file = new FileInfo(path);
        if (file.Exists)
        {
            file.Delete();
        }
        CreatEmptyExcel2003(path);

        //写入数据,覆盖性写入,原本的数据将被覆盖
        HSSFWorkbook hssfworkbook = new HSSFWorkbook();
        //sheet
        ISheet sheet = hssfworkbook.CreateSheet(sheetName);
        //
        IRow row;
        //单元
        ICell cell;

        for (int i = 0; i < position.Length; i++)
        {
            row = sheet.GetRow(position[i][0]);
            if (row == null)
            {
                row = sheet.CreateRow(position[i][0]);
            }
            cell = row.GetCell(position[i][1]);
            if (cell == null)
            {
                cell = row.CreateCell(position[i][1]);
            }
            cell.SetCellValue(text[i]);
        }

        MemoryStream stream = new MemoryStream();
        hssfworkbook.Write(stream);
        var buf = stream.ToArray();


        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
        fs.Write(buf, 0, buf.Length);
        fs.Flush();
        fs.Close();
    }

    /// <summary>
    /// 指定路径创建空Excel,路径中包含了excel名称
    /// </summary>
    /// <param name="excelPath">带有路径的Excel名称</param>
    public static void CreatEmptyExcel2003(string excelPath)
    {
        File.Copy(EmptyExcel2003, excelPath);
    }

    private static object GetValueTypeForXLS(HSSFCell cell)
    {
        if (cell == null)
            return null;
        switch (cell.CellType)
        {
            case CellType.Blank: //BLANK:  
                return null;
            case CellType.Boolean: //BOOLEAN:  
                return cell.BooleanCellValue;
            case CellType.Numeric: //NUMERIC:  
                return cell.NumericCellValue;
            case CellType.String: //STRING:  
                return cell.StringCellValue;
            case CellType.Error: //ERROR:  
                return cell.ErrorCellValue;
            case CellType.Formula: //FORMULA:  
            default:
                return "=" + cell.CellFormula;
        }
    }
    #endregion

 


版权声明:本文为weixin_42275416原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。