• C# 文件导出 EXCEL



    #region 读excel
                //using (Stream stream = File.OpenRead(@"c:\Book1.xls"))
                //{
                //    //内存中的excel文件
                //    HSSFWorkbook workBook = new HSSFWorkbook(stream);
                //    //读取sheet
                //    HSSFSheet sheet = workBook.GetSheetAt(0);

                //    //Console.WriteLine(sheet.GetRow(0).GetCell(0).NumericCellValue);
                //    StringBuilder sb = new StringBuilder();

                //    for (int i = 1; i <= sheet.LastRowNum; i++)
                //    {
                //        //行
                //        HSSFRow row = sheet.GetRow(i);
                //        //遍历列
                //        for (int j = 0; j < row.LastCellNum; j++)
                //        {
                //            HSSFCell cell = row.GetCell(j);
                //            //判断单元格的类型。分别用不通类型读取单元格的值
                //            if (cell == null)
                //            {
                //                sb.Append("");
                //                continue;
                //            }
                //            if (cell.CellType == HSSFCell.CELL_TYPE_STRING)
                //            {
                //                sb.Append(cell.StringCellValue);
                //            }
                //            else if(cell.CellType == HSSFCell.CELL_TYPE_NUMERIC)
                //            {
                //                sb.Append(cell.NumericCellValue);
                //            }
                //        }
                //        sb.Append("\r\n");
                //    }
                //    Console.WriteLine(sb.ToString());
                //}
    #endregion


                #region 创建excel
                ////创建excel
                ////在内存中创建了一个空的excel文件
                //HSSFWorkbook workBook = new HSSFWorkbook();
                ////在workBook中创建一个sheet
                //HSSFSheet sheet = workBook.CreateSheet();
                ////sheet中创建行
                //HSSFRow row = sheet.CreateRow(0);
                ////创建cell 并设置cell的类型
                //HSSFCell cell = row.CreateCell(0,HSSFCell.CELL_TYPE_STRING);
                //cell.SetCellValue("15028008001");
                ////
                //cell = row.CreateCell(1, HSSFCell.CELL_TYPE_NUMERIC);
                //cell.SetCellValue(456);

                //using (Stream stream = File.OpenWrite("c:\\1111.xls"))
                //{
                //    workBook.Write(stream);
                //}
                //Console.WriteLine("chenggong ");
    #endregion

    转载请注明出处,感谢。
    作者:李宏旭
    阅罢此文,如果您觉得本文不错并有所收获,请【打赏】或【推荐】,也可【评论】留下您的问题或建议与我交流。
    你的支持是我不断创作和分享的不竭动力!
  • 相关阅读:
    .Net下HTTP访问穿越多层代理的方法以及代理服务器的验证 转载
    SB淘宝api的奇葩问题! 一则服务器无法访问淘宝api
    C# 系统应用之清除Cookies、IE临时文件、历史记录 转载
    Replication--进程无法在“xxxx”上执行“sp_replcmds”
    [leetcode] Search Insert Position
    [leetcode] Search for a Range
    [leetcode] Merge Sorted Array
    [leetcode] Remove Element
    [leetcode] Find Minimum in Rotated Sorted Array
    [leetcode] Container With Most Water
  • 原文地址:https://www.cnblogs.com/bjlhx/p/2265817.html
Copyright © 2020-2023  润新知