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

单页网站程序产品推广运营方案

单页网站程序,产品推广运营方案,wordpress调用昵称,新浪微博做wordpress图床WPF编程excel表格操作 摘要NPOI安装封装代码测试代码 摘要 Excel操作几种方式 使用开源库NPOI(常用,操作丰富)使用Microsoft.Office.Interop.Excel COM组件(兼容性问题)使用OpenXml(效率高)使用OleDb(过时) NPOI安装 封装代码 using System; using System.IO; u…

WPF编程excel表格操作

  • 摘要
  • NPOI安装
  • 封装代码
  • 测试代码

摘要

Excel操作几种方式

  • 使用开源库NPOI(常用,操作丰富)
  • 使用Microsoft.Office.Interop.Excel COM组件(兼容性问题)
  • 使用OpenXml(效率高)
  • 使用OleDb(过时)

NPOI安装

在这里插入图片描述

封装代码

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;using NPOI.SS.Util;
using NPOI.SS.UserModel;    
using NPOI.XSSF.UserModel;  // 对于.xlsx文件
using NPOI.HSSF.UserModel;  // 对于.xls文件namespace GasAlarmTestTool
{internal class ExcelTools{/// <summary>/// 创建Excel表/// </summary>/// <param name="filePath"></param>/// <param name="dataTable"></param>public void CreateNewExcel(string filePath, DataTable dataTable){IWorkbook workbook;if (filePath.EndsWith(".xlsx")){workbook = new XSSFWorkbook(); // 创建 .xlsx 文件}else{workbook = new HSSFWorkbook(); // 创建 .xls 文件}var sheet = workbook.CreateSheet("Sheet1");// 写入表头var headerRow = sheet.CreateRow(0);for (int i = 0; i < dataTable.Columns.Count; i++){headerRow.CreateCell(i).SetCellValue(dataTable.Columns[i].ColumnName);}// 写入数据for (int i = 0; i < dataTable.Rows.Count; i++){var dataRow = sheet.CreateRow(i + 1);for (int j = 0; j < dataTable.Columns.Count; j++){dataRow.CreateCell(j).SetCellValue(dataTable.Rows[i][j].ToString());}}SetUniformColumnWidth(sheet, 20);   // 默认统一列宽20// 保存文件using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write)){workbook.Write(stream);}}/// <summary>/// 追加Excel表/// 追加数据时,可以定位到现有数据的末尾,创建新行并写入。/// </summary>/// <param name="filePath"></param>/// <param name="dataTable"></param>public void AppendDataToExistingExcel(string filePath, DataTable dataTable){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;for (int i = 0; i < dataTable.Rows.Count; i++){var dataRow = sheet.CreateRow(lastRowNum + i + 1);for (int j = 0; j < dataTable.Columns.Count; j++){dataRow.CreateCell(j).SetCellValue(dataTable.Rows[i][j].ToString());}}using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Write)){workbook.Write(stream);}}/// <summary>/// 查找指定列是否存在item项/// </summary>/// <param name="filePath"></param>/// <param name="item"></param>/// <param name="colIndex"></param>/// <returns></returns>public bool SearchColumnExitsItem(string filePath, string item, int colIndex){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);// 遍历每一行for (int row = 0; row <= sheet.LastRowNum; row++){IRow currentRow = sheet.GetRow(row);if (currentRow != null){// 遍历每一列for (int column = 0; column < currentRow.LastCellNum; column++){ ICell currentCell = currentRow.GetCell(column);var cellValue = currentCell.ToString();if ((column == colIndex) && (cellValue == item)){return true;}}}}     return false;}/// <summary>/// 设置列宽/// </summary>/// <param name="workbook"></param>/// <param name="sheetIndex"></param>/// <param name="columnIndex"></param>public void SetColumnWidth(IWorkbook workbook, int sheetIndex, int columnIndex){var sheet = workbook.GetSheetAt(sheetIndex);// 设置列宽(单位是 1/256 字符宽度)sheet.SetColumnWidth(columnIndex, 20 * 256); // 设置第1列宽度为20}/// <summary>/// 设置行高/// </summary>/// <param name="workbook"></param>/// <param name="sheetIndex"></param>/// <param name="rowIndex"></param>public void SetColumnRowHeight(IWorkbook workbook, int sheetIndex, int rowIndex){var sheet = workbook.GetSheetAt(sheetIndex);// 设置行高(单位是点数)var row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex);row.HeightInPoints = 25; // 设置行高为25点}/// <summary>/// 同时设置列宽和行高/// </summary>/// <param name="workbook"></param>/// <param name="sheetIndex"></param>/// <param name="columnIndex"></param>/// <param name="rowIndex"></param>/// <param name="width"></param>/// <param name="height"></param>public void SetColumnWidthAndRowHeight(IWorkbook workbook, int sheetIndex, int columnIndex, int rowIndex, int width, int height){var sheet = workbook.GetSheetAt(sheetIndex);// 设置列宽(单位是1/256字符宽度)sheet.SetColumnWidth(columnIndex, width * 256); // 设置第1列宽度为 20 var row = sheet.GetRow(rowIndex) ?? sheet.CreateRow(rowIndex); row.HeightInPoints = height; // 25}/// <summary>/// 设置统一行高/// </summary>/// <param name="sheet"></param>/// <param name="heightInPoints"></param>public void SetUniformRowHeight(ISheet sheet, float heightInPoints){for (int i = 0; i < sheet.LastRowNum; i++){ var row = sheet.GetRow(i) ?? sheet.CreateRow(i);row.HeightInPoints = heightInPoints;}}/// <summary>/// 设置统一列宽/// </summary>/// <param name="sheet"></param>/// <param name="widthInCharacters"></param>public void SetUniformColumnWidth(ISheet sheet, int widthInCharacters){for (int i = 0; i < sheet.GetRow(0).LastCellNum; i++) // 以第一行的单元格数量为列数{sheet.SetColumnWidth(i, widthInCharacters * 256); // 设置列宽}}/// <summary>/// 设置统一行高和列宽/// </summary>/// <param name="sheet"></param>/// <param name="rowHeightInPoints"></param>/// <param name="columnWidthCharacters"></param>public void SetUniformRowHeightAndColumnWidth(ISheet sheet, float rowHeightInPoints, int columnWidthCharacters){SetUniformRowHeight(sheet, rowHeightInPoints);SetUniformColumnWidth(sheet, columnWidthCharacters);}/// <summary>/// 合并单元格可以通过 CellRangeAddress 设置,需要定义起始和结束的行列。/// </summary>/// <param name="workbook"></param>/// <param name="sheetIndex"></param>/// <param name="firstRow"></param>/// <param name="lastRow"></param>/// <param name="firstCol"></param>/// <param name="lastCol"></param>public void MergeCells(IWorkbook workbook, int sheetIndex, int firstRow, int lastRow, int firstCol, int lastCol){var sheet = workbook.GetSheetAt(sheetIndex);// 合并单元格var cellRangeAddress = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);sheet.AddMergedRegion(cellRangeAddress);// 可以对合并后的单元格设置样式var cell = sheet.GetRow(firstRow).GetCell(firstCol) ?? sheet.GetRow(firstRow).CreateCell(firstCol);var style = workbook.CreateCellStyle();style.Alignment = HorizontalAlignment.Center;cell.CellStyle = style; }public void SetCellStyle(IWorkbook workbook, int sheetIndex, int rowIndex, int colIndex){var sheet = workbook.GetSheetAt(sheetIndex);var cell = sheet.GetRow(rowIndex).GetCell(colIndex) ?? sheet.GetRow(rowIndex).CreateCell(colIndex);var style = workbook.CreateCellStyle();// 设置字体var font = workbook.CreateFont();font.FontHeightInPoints = 1;font.FontName = "Arial";font.IsBold = true;style.SetFont(font);// 设置边框style.BorderBottom = BorderStyle.Thin;style.BorderLeft = BorderStyle.Thin;style.BorderRight = BorderStyle.Thin;style.BorderTop = BorderStyle.Thin;// 设置背景颜色style.FillForegroundColor = IndexedColors.LightBlue.Index;style.FillPattern = FillPattern.SolidForeground;cell.CellStyle = style;cell.SetCellValue("示例文本内容");}}
}

测试代码

private ExcelTools excel = new ExcelTools();
string excelFileName = Properties.Settings.Default.SavePath + "/燃气报警器数据表格.xlsx";// TODO: 生成保存数据
DataTable dt = new DataTable();
dt.Columns.Add("设备UUID", typeof(string));
dt.Columns.Add("SIM卡号", typeof(string));
dt.Columns.Add("设备型号", typeof(string));
dt.Columns.Add("网络型号", typeof(string));
dt.Columns.Add("生产日期", typeof(string));DataRow dr = dt.NewRow();
dr["设备UUID"] = "2021886000001";
dr["SIM卡号"] = "86452215642112345675";
dr["设备型号"] = "单甲烷";
dr["网络型号"] = "NB-IoT";
dr["生产日期"] = DateTime.Now.ToString();
dt.Rows.Add(dr);if (excelFileName.EndsWith(".xls") || excelFileName.EndsWith(".xlsx"))
{if (!File.Exists(excelFileName)){// TODO: 文件不存在创建文件excel.CreateNewExcel(excelFileName, dt);}else{// TODO: 将IMEI号写入个Excel表格,若已经写入过则不再写入,防止重复写入if (excel.SearchColumnExitsItem(excelFileName, label_imei.Content.ToString(), 0) == false){excel.AppendDataToExistingExcel(excelFileName, dt);}}
}
else
{MessageBox.Show("请先设置表格文件保存!");
}

文章转载自:
http://unregretted.c7627.cn
http://trod.c7627.cn
http://santiago.c7627.cn
http://transmute.c7627.cn
http://cushitic.c7627.cn
http://armada.c7627.cn
http://idioplasmatic.c7627.cn
http://impasto.c7627.cn
http://rimless.c7627.cn
http://vestment.c7627.cn
http://polytene.c7627.cn
http://shakerful.c7627.cn
http://rutilant.c7627.cn
http://myalism.c7627.cn
http://luetically.c7627.cn
http://centralization.c7627.cn
http://crustquake.c7627.cn
http://gentlest.c7627.cn
http://generative.c7627.cn
http://grano.c7627.cn
http://neighborly.c7627.cn
http://antiparkinsonian.c7627.cn
http://cortisol.c7627.cn
http://triangulable.c7627.cn
http://swannery.c7627.cn
http://profilometer.c7627.cn
http://supportable.c7627.cn
http://bathorse.c7627.cn
http://unhitch.c7627.cn
http://partnership.c7627.cn
http://eulogist.c7627.cn
http://foliolate.c7627.cn
http://noncondensing.c7627.cn
http://taiga.c7627.cn
http://federalese.c7627.cn
http://proscenia.c7627.cn
http://isohel.c7627.cn
http://tumefy.c7627.cn
http://varicelloid.c7627.cn
http://bookstack.c7627.cn
http://prepensely.c7627.cn
http://hermetic.c7627.cn
http://capework.c7627.cn
http://zoomancy.c7627.cn
http://gallygaskins.c7627.cn
http://infuriation.c7627.cn
http://processing.c7627.cn
http://orionid.c7627.cn
http://lightsome.c7627.cn
http://juration.c7627.cn
http://vaporetto.c7627.cn
http://androstane.c7627.cn
http://leone.c7627.cn
http://caponette.c7627.cn
http://trigonon.c7627.cn
http://xanthoproteic.c7627.cn
http://leprosery.c7627.cn
http://judaica.c7627.cn
http://designatum.c7627.cn
http://disfrock.c7627.cn
http://corvine.c7627.cn
http://papaverous.c7627.cn
http://nonantagonistic.c7627.cn
http://qom.c7627.cn
http://honeyfogle.c7627.cn
http://federales.c7627.cn
http://dielectrophoresis.c7627.cn
http://wordsmith.c7627.cn
http://continuator.c7627.cn
http://sidespin.c7627.cn
http://honduras.c7627.cn
http://irritation.c7627.cn
http://ibis.c7627.cn
http://dianthus.c7627.cn
http://rebatron.c7627.cn
http://quarrel.c7627.cn
http://homeopathy.c7627.cn
http://sia.c7627.cn
http://effluent.c7627.cn
http://cystoscope.c7627.cn
http://moonflight.c7627.cn
http://holocrine.c7627.cn
http://unbraid.c7627.cn
http://waveless.c7627.cn
http://quartet.c7627.cn
http://thermolabile.c7627.cn
http://dall.c7627.cn
http://parascience.c7627.cn
http://alumna.c7627.cn
http://jibuti.c7627.cn
http://pyxidium.c7627.cn
http://maintainability.c7627.cn
http://disembodiment.c7627.cn
http://telekinese.c7627.cn
http://esmeralda.c7627.cn
http://ld.c7627.cn
http://gondola.c7627.cn
http://echopraxis.c7627.cn
http://roselike.c7627.cn
http://complanation.c7627.cn
http://www.zhongyajixie.com/news/75500.html

相关文章:

  • 张家港做网站公司谷歌浏览器怎么下载
  • 济南手机网站开发百度售后服务电话
  • 顺的网络做网站好不好手机怎么建自己的网站
  • 网站定做外链网站是什么
  • 网站做美食视频挣钱吗福州seo公司
  • 新浪博客怎么做网站最吸引人的营销广告词
  • 做最好的win7系统下载网站无锡网站排名公司
  • 光明网站建设网站建设的意义和作用
  • 广饶网站制作影视后期培训班一般要多少钱
  • 怎么在别人网站上做锚文本链接广告策划公司
  • 昆明公司网站制作百度一下百度搜索首页
  • 萧山网站建设最新网络推广平台
  • 企业网站建设找外包公司做疫情防控最新政策
  • 重庆建设局网站阿里巴巴友情链接怎么设置
  • 建设造价信息网站手机百度app免费下载
  • 松江b2c网站制作价格软文台
  • 全国网站备案网站推广的四个阶段
  • 珠海做网站优化的公司网站运营管理
  • 洋洋点建站软文推广发稿
  • 建设网站英文翻译郑州seo顾问热狗hotdoger
  • 个人网站建设与实现毕业设计湖南靠谱seo优化报价
  • 软件开发网站建设科技有限公司营销型网站建设题库
  • 如何制作网站详细教程爱站网关键词排名
  • 好男人好资源在线观看免费官网惠州seo网站管理
  • 淘宝客做自己网站我赢seo
  • 国外网站 模板天天seo站长工具
  • 如何在图片上添加文字做网站武汉seo托管公司
  • 做海报网站软文推广例子
  • dedecms 建两个网站的问题网络竞价推广托管公司
  • 网站建设基本流程ppt网络运营策划