当前位置: 首页 > news >正文

广州知名网站建设哪家公司好网站案例分析

广州知名网站建设哪家公司好,网站案例分析,福安做网站最好,wordpress 繁體今天在弄使用npoi对excel表的操作,遇到个问题就是使用workbook通过filestream打开后,让后workbook.write(filestream)居然报文件流关闭了,无法写入,弄了好久都不行,最后通过写2个excel文件来解决,现在看来我…
  1. 今天在弄使用npoi对excel表的操作,遇到个问题就是使用workbook通过filestream打开后,让后workbook.write(filestream)居然报文件流关闭了,无法写入,弄了好久都不行,最后通过写2个excel文件来解决,现在看来我使用 HSSFWorkbook workbook = new HSSFWorkbook(new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite));这种读写模式有问题,使用这种  using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read));既可以读,又可以写。

public void AppendDataToExistingExcel(string filePath)

        {

            try

            {

                IWorkbook workbook;

                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))

                {

                    workbook = filePath.EndsWith(".xlsx") ? (IWorkbook)new XSSFWorkbook(stream) : new HSSFWorkbook(stream);

                }

                var sheet = workbook.GetSheetAt(0);

                int lastRowNum = sheet.LastRowNum;

                sheet.GetRow(0).CreateCell(0).SetCellValue("hahaha");

                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Write))

                {

                    workbook.Write(stream);

                }

            }

            catch (Exception ex) {}

        }

  1. Npoi的操作

New一个workbook,workbook通过createSheet()或者getSheetAt(index)获取sheet页面

sheet通过createRow(rowIndex)或者getRow(indexIndex),获取或者创建行

Sheet.lastRownum可以获取所有行

IRow row = sheet.createRow(rowIndex);获取行

Row.capacity 或者row.cells.count获取每一行的列个数,注意的是没有sheet的列个数,只有行的列个数

当向一个行中的一个单元格写数据的时候row.createCell(colIndex),cell.setCellValue(),如果没有向一个单元格写过数据,那么row.getCell()会返回null的

  1. Cell样式

注意style通过workbook.createStyle(),而不是通过其他获得

有的api有font.isBold =true;但是我这个没有只有通过font.BoldWeight=800来设置,注意是对每一个cell来设置样式 cell.cellStyle= style;也就是for循环行和列来设置样式,

  1. 合并单元格

只需要new CellRangeAddress()即可,然后sheet.addMergendRegion(cellRangeAddress),这样就合并了,但是合并后,需要写入信息,以及重新设置样式

public class WZExcelUtil
    {
        /// <summary>
        /// 设置一般表格样式
        /// </summary>
        /// <param name="workbook"></param>
        /// <param name="sheetIndex"></param>
        public void setNormalCellStyle(IWorkbook workbook, int sheetIndex)
        {

            ICellStyle style = workbook.CreateCellStyle();
            var font = workbook.CreateFont();
            font.FontHeightInPoints = 12;
            //font.FontName = "Arial";
            font.FontName = "仿宋";
            style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            style.SetFont(font);

            ISheet sheet = workbook.GetSheetAt(sheetIndex);
            int rowNum = sheet.LastRowNum;
            for (int rowIndex = 0; rowIndex < rowNum + 1; rowIndex++)
            {
                IRow row = sheet.GetRow(rowIndex);
                //int cols = row.Cells.Capacity;
                int cols = row.Cells.Count;
                for (int colIndex = 0; colIndex < cols; colIndex++)
                {
                    var cell = sheet.GetRow(rowIndex).GetCell(colIndex);
                    cell.CellStyle = style;
                    sheet.SetColumnWidth(colIndex, 15 * 256); // 设置第 1 列宽度为 20
                }
                row.HeightInPoints = 25; // 设置行高为 25 点
            }
        }

        /// <summary>
        /// 设置title表格样式
        /// </summary>
        /// <param name="workbook"></param>
        /// <param name="sheetIndex"></param>
        /// <param name="headTitleIndex"></param>
        public void setHeadTitleStyle(IWorkbook workbook, int sheetIndex, int headTitleIndex)
        {
            ISheet sheet = workbook.GetSheetAt(sheetIndex);

            ICellStyle style = workbook.CreateCellStyle();
            var font = workbook.CreateFont();
            font.FontHeightInPoints = 14;
            //font.FontName = "Arial";
            font.FontName = "仿宋";
            font.Boldweight = 800;
            style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            style.SetFont(font);

            IRow headRow = sheet.GetRow(headTitleIndex);
            int cols = headRow.Cells.Count;
            for (int colIndex = 0; colIndex < cols; colIndex++)
            {
                var cell = headRow.GetCell(colIndex);
                cell.CellStyle = style;
            }
        }

        /// <summary>
        /// 设置合并单元格
        /// </summary>
        /// <param name="workbook"></param>
        /// <param name="sheetIndex"></param>
        /// <param name="picTitleRowIndex"> 内容行索引 </param>
        /// <param name="picTitleColIndex"> 内容列索引</param>
        /// <param name="titleInfo"> 显示信息</param>
        /// <param name="firstRow"></param>
        /// <param name="lastRow"></param>
        /// <param name="firstCol"></param>
        /// <param name="lastCol"></param>
        private void setMergeInfo(IWorkbook workbook, int sheetIndex, int picTitleRowIndex, int picTitleColIndex, string titleInfo,
            int firstRow, int lastRow, int firstCol, int lastCol)
        {
            ISheet sheet = workbook.GetSheetAt(sheetIndex);
            CellRangeAddress cellRangeAddress = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
            sheet.AddMergedRegion(cellRangeAddress);
            //sheet.GetRow(picTitleRowIndex).GetCell(picTitleColIndex).SetCellValue(titleInfo);
            IRow row = sheet.GetRow(picTitleRowIndex);
            ICell cell = row.CreateCell(picTitleColIndex); ///getCell(picTitleColIndex);失败,需要create
            cell.SetCellValue(titleInfo);

            ICellStyle style = workbook.CreateCellStyle();
            var font = workbook.CreateFont();
            font.FontHeightInPoints = 14;
            //font.FontName = "Arial";
            font.FontName = "仿宋";
            font.Boldweight = 800;
            style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
            style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            style.SetFont(font);
            cell.CellStyle = style;

        }


        private void insertPic(IWorkbook workbook, ISheet sheet,string picpath,int rowleft,int rowright,int colleft,int colright)
        {
            //string picpath = "C:\\Users\\wangzg\\Desktop\\ceshi.png";

            //第一步 获取图片bytes字节
            byte[] bytes = File.ReadAllBytes(picpath);

            //第二步 确定图片索引,注意图片类型
            int ip = workbook.AddPicture(bytes, PictureType.PNG);
            //第三步 创建画布
            IDrawing drawing = sheet.CreateDrawingPatriarch();
            //第三步 创建锚点
            IClientAnchor anchor = workbook.GetCreationHelper().CreateClientAnchor();
            //第四步 设置锚点左上角  右下角 也就是图片的大小,不过是通过左上点  和右下点来得到的
            //anchor.Row1 = 2;
            //anchor.Col1 = 1;
            //anchor.Row2 = 12;
            //anchor.Col2 = 12;
            anchor.Row1 = rowleft;
            anchor.Col1 = colleft;
            anchor.Row2 = rowright;
            anchor.Col2 = colright;
            //第五步 把图片插入到相应位置
            IPicture picture = drawing.CreatePicture(anchor, ip);

        }
    }

private void button12_Click(object sender, EventArgs e)
        {
            try
            {
                string[] headtitle = { "aaaa", "bbb", "ccc", "ddd", "eee" };
                List<List<string>> lists = new List<List<string>>();
                string target = "C:\\Users\\wangzg\\Desktop\\112_4.xls";
                IWorkbook workbook = target.EndsWith(".xlsx") ? (IWorkbook)new XSSFWorkbook() : new HSSFWorkbook();
                ISheet sheet = workbook.CreateSheet("taoya");

                int headTitleRowIndex = 15;

                for (int i = 0; i < 15; i++) {
                    sheet.CreateRow(i);
                }

                //表头
                IRow headRow = sheet.CreateRow(headTitleRowIndex);
                for (int i = 0; i < headtitle.Length; i++)
                {
                    headRow.CreateCell(i).SetCellValue(headtitle[i]);
                }
                //列值
                for (int i = 0; i < 10; i++)
                {
                    List<string> oil = new List<string>();
                    oil.Add("aa" + i);
                    oil.Add("bb" + i);
                    oil.Add("cc" + i);
                    oil.Add("dd" + i);
                    oil.Add("ee" + i);
                    lists.Add(oil);
                }

                for (int i = 0; i < lists.Count; i++)
                {
                    IRow row = sheet.CreateRow(sheet.LastRowNum + 1);
                    List<string> objs = lists[i];
                    for (int j = 0; j < headtitle.Length; j++)
                    {
                        row.CreateCell(j).SetCellValue(objs[j]);
                    }
                }

                setNormalCellStyle(workbook, 0);
                setHeadTitleStyle(workbook, 0, headTitleRowIndex);
                setMergeInfo(workbook, 0,1,1, "aaaa");
                insertPic(workbook, workbook.GetSheetAt(0));
                using (var stream = new FileStream(target, FileMode.Create, FileAccess.Write))
                {
                    workbook.Write(stream);
                }

                MessageBox.Show("ok");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


文章转载自:
http://ptolemaism.c7498.cn
http://semimystical.c7498.cn
http://antiparasitic.c7498.cn
http://nary.c7498.cn
http://crusty.c7498.cn
http://unclothe.c7498.cn
http://cram.c7498.cn
http://believable.c7498.cn
http://unifoliate.c7498.cn
http://earwitness.c7498.cn
http://centralise.c7498.cn
http://euglenid.c7498.cn
http://tribromoethanol.c7498.cn
http://chevet.c7498.cn
http://ingestible.c7498.cn
http://howdah.c7498.cn
http://menad.c7498.cn
http://trochili.c7498.cn
http://kashmirian.c7498.cn
http://anfractuous.c7498.cn
http://palynology.c7498.cn
http://mustang.c7498.cn
http://ferryboat.c7498.cn
http://sleave.c7498.cn
http://villiform.c7498.cn
http://lz.c7498.cn
http://kynewulf.c7498.cn
http://scold.c7498.cn
http://foamy.c7498.cn
http://pastorale.c7498.cn
http://comique.c7498.cn
http://burns.c7498.cn
http://lyssa.c7498.cn
http://inflict.c7498.cn
http://operant.c7498.cn
http://ultralight.c7498.cn
http://grandducal.c7498.cn
http://sapphirine.c7498.cn
http://coif.c7498.cn
http://oldie.c7498.cn
http://noncooperation.c7498.cn
http://inexplicably.c7498.cn
http://autoptical.c7498.cn
http://farmland.c7498.cn
http://suprematism.c7498.cn
http://counterdemonstrate.c7498.cn
http://americanophobia.c7498.cn
http://brainwave.c7498.cn
http://accustomed.c7498.cn
http://overfold.c7498.cn
http://lawmaking.c7498.cn
http://typewriter.c7498.cn
http://woozy.c7498.cn
http://hydration.c7498.cn
http://germinator.c7498.cn
http://sclerodactylia.c7498.cn
http://determinator.c7498.cn
http://alluvion.c7498.cn
http://reap.c7498.cn
http://maximal.c7498.cn
http://sharefarmer.c7498.cn
http://preoption.c7498.cn
http://galingale.c7498.cn
http://demyelinate.c7498.cn
http://tarantass.c7498.cn
http://adjustive.c7498.cn
http://linter.c7498.cn
http://interactant.c7498.cn
http://potiphar.c7498.cn
http://meteoric.c7498.cn
http://counterstain.c7498.cn
http://antirachitic.c7498.cn
http://communicative.c7498.cn
http://haram.c7498.cn
http://gso.c7498.cn
http://rectilineal.c7498.cn
http://hypogonadism.c7498.cn
http://hewer.c7498.cn
http://mnemotechnics.c7498.cn
http://artiodactylous.c7498.cn
http://andalusia.c7498.cn
http://ovulation.c7498.cn
http://rhizocephalan.c7498.cn
http://pna.c7498.cn
http://dendrochronology.c7498.cn
http://taxpaying.c7498.cn
http://altarage.c7498.cn
http://puisne.c7498.cn
http://zaftig.c7498.cn
http://kazakh.c7498.cn
http://trading.c7498.cn
http://narcose.c7498.cn
http://muskhogean.c7498.cn
http://sarcomatosis.c7498.cn
http://acceptability.c7498.cn
http://satyagraha.c7498.cn
http://whithersoever.c7498.cn
http://runless.c7498.cn
http://comorin.c7498.cn
http://fractographic.c7498.cn
http://www.zhongyajixie.com/news/52643.html

相关文章:

  • 网站管理员怎么联系国外独立站网站
  • 网站建设优化服务器国际新闻报道
  • 葫芦岛网站建设做推广的公司一般都叫什么
  • 招聘网站怎么做营销小江seo
  • 宜宾网站建设公司网络营销推广方法和手段
  • 做个外贸的网站不懂英语咋做网站推广优化业务
  • cnnic 是什么网站绍兴seo
  • 带地板翻转的网站怎么做百度推广技巧方法
  • 重庆做企业网站长春网站制作推广
  • 做网站效果怎么样那种网站怎么搜关键词
  • 让别人做网站多久开始注册域名最新营销模式有哪些
  • 企业网站制作收费营销软文200字
  • 青岛网站美工登封网络推广公司
  • icp备案 网站服务内容东莞网络优化调查公司
  • 中国住建部网站官网营销型企业网站建设的内容
  • 济南外贸建站体验营销案例
  • 前端开发线上培训焦作关键词优化排名
  • 笨鸟网站开发企业网站开发制作
  • 阿坝网站建设新浪体育nba
  • 网站建设可行性研究报告百度seo快速排名优化服务
  • 硅胶 技术支持 东莞网站建设南宁seo全网营销
  • 聊城做网站费用价格引擎搜索技巧
  • 做网站联盟黄页网络的推广软件
  • 专业政府网站建设公司郑州十大外贸电商平台
  • 做网站能挣钱么怎么给自己的公司建立网站
  • 可以做公司宣传的网站有哪些武汉seo创造者
  • 网站里面发消息怎么做超链接seo刷点击软件
  • 网站图片滚动是怎么做的关键词有哪些关联词
  • 网站建设优化两千字夸克搜索
  • 中国站长之家官网顾问