首页 > 数据库 >将Execl表格的数据导入数据库

将Execl表格的数据导入数据库

时间:2023-09-21 12:13:12浏览次数:58  
标签:Execl cell 表格 startRow 导入 CellType model cellValue endRow

/// 导入台区
/// </summary>
/// <param name="savePath"></param>
/// <param name="msg"></param>
/// <returns></returns>
public bool Upload(string savePath, out string msg)
{
msg = "上传成功";

DataService.Model.yq_biaoji_excel model = new DataService.Model.yq_biaoji_excel();
//List<List<string>> data = new List<List<string>>();

using (FileStream stream = new FileStream(savePath, FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = new XSSFWorkbook(stream);

var sheetNames = "";
if (savePath.Contains("台区1"))
{
model.tq_code = "01";
}
else if (savePath.Contains("台区2"))
{
model.tq_code = "02";
}
else if (savePath.Contains("台区3"))
{
model.tq_code = "03";
}
else if (savePath.Contains("台区4"))
{
model.tq_code = "04";
}
else if (savePath.Contains("台区5"))
{
model.tq_code = "05";
}
var query = new StringBuilder(" 1 = 1 ");

query.AppendFormat(" AND tq_code LIKE '%{0}%' ", model.tq_code.Trim());

var list = bll_biaoji_excel.GetModelList(query.ToString());

if (list.Count != 0)
{
foreach (var item in list)
{
bll_biaoji_excel.Delete(item.id);
}

for (int i = 0; i < workbook.NumberOfSheets; i++)
{

ISheet sheet = workbook.GetSheetAt(i);


int startRow = -1; // 起始行
int endRow = -1; // 结束行
startRow = -1;
endRow = -1;

for (int rowIndex = 0; rowIndex <= sheet.LastRowNum; rowIndex++)
{
IRow row = sheet.GetRow(rowIndex);

if (row != null)
{
ICell cell = row.GetCell(0);
string cellValue = "";

if (cell != null)
{
switch (cell.CellType)
{
case CellType.Numeric:
cellValue = cell.NumericCellValue.ToString();
break;
case CellType.String:
cellValue = cell.StringCellValue;
break;
case CellType.Boolean:
cellValue = cell.BooleanCellValue.ToString();
break;
case CellType.Formula:
cellValue = cell.CellFormula;
break;
default:
cellValue = "";
break;
}
}
Console.WriteLine("单元格值:" + cellValue);
//if (cellValue == a1 || cellValue == a2 || cellValue == a3 || cellValue == a4 || cellValue == a5 || cellValue == a6 || cellValue == a7 || cellValue == a8 || cellValue == a9 || cellValue == a10 || cellValue == a11 || cellValue == a12
// //|| cellValue == a12 || cellValue == a13 || cellValue == a14 || cellValue == a15 || cellValue == a16 || cellValue == a17 || cellValue == a18
// )
if (cellValue.Contains("&"))
{
startRow = rowIndex + 1;
model.bj_module = cellValue.Replace("&", "");
endRow = rowIndex + 1; // 初始化为相同的值
if (cellValue == "&日冻结-日冻结示值")
{
startRow = startRow + 1;
}
if (cellValue == "&日冻结-曲线数据")
{
startRow = startRow + 1;
}
// 寻找结束行
for (int nextRowIndex = rowIndex + 1; nextRowIndex <= sheet.LastRowNum; nextRowIndex++)
{
IRow nextRow = sheet.GetRow(nextRowIndex);
if (nextRow != null)
{

ICell nextCell = nextRow.GetCell(0);

if (nextCell != null)
{
if (nextCell.IsMergedCell)
{
endRow = nextRowIndex + 2;
}
else
{
string nextCellValue = nextCell.CellType == CellType.Numeric ? nextCell.NumericCellValue.ToString() : nextCell.StringCellValue;
if (nextCellValue != "*")
{
endRow = nextRowIndex + 1;
}
else
{
break;
}
}

}
}
}

sheetNames = sheet.SheetName;
model.biaoji = sheetNames;
model.isfirst = "1";
model.id = BasePage.GetNewId();
model.start_index = startRow;
model.end_index = endRow - 1;
Console.WriteLine("起始行:" + startRow);
Console.WriteLine("结束行:" + endRow);
bll_biaoji_excel.Add(model);
}

 


}

 

}
}
}
else
{
for (int i = 0; i < workbook.NumberOfSheets; i++)
{

ISheet sheet = workbook.GetSheetAt(i);


int startRow = -1; // 起始行
int endRow = -1; // 结束行
startRow = -1;
endRow = -1;

for (int rowIndex = 0; rowIndex <= sheet.LastRowNum; rowIndex++)
{
IRow row = sheet.GetRow(rowIndex);

if (row != null)
{
ICell cell = row.GetCell(0);
string cellValue = "";

if (cell != null)
{
switch (cell.CellType)
{
case CellType.Numeric:
cellValue = cell.NumericCellValue.ToString();
break;
case CellType.String:
cellValue = cell.StringCellValue;
break;
case CellType.Boolean:
cellValue = cell.BooleanCellValue.ToString();
break;
case CellType.Formula:
cellValue = cell.CellFormula;
break;
default:
cellValue = "";
break;
}
}
Console.WriteLine("单元格值:" + cellValue);
//if (cellValue == a1 || cellValue == a2 || cellValue == a3 || cellValue == a4 || cellValue == a5 || cellValue == a6 || cellValue == a7 || cellValue == a8 || cellValue == a9 || cellValue == a10 || cellValue == a11 || cellValue == a12
// //|| cellValue == a12 || cellValue == a13 || cellValue == a14 || cellValue == a15 || cellValue == a16 || cellValue == a17 || cellValue == a18
// )
if (cellValue.Contains("&"))
{
startRow = rowIndex + 1;
model.bj_module = cellValue.Replace("&", "");
endRow = rowIndex + 1; // 初始化为相同的值
if (cellValue == "&日冻结-日冻结示值")
{
startRow = startRow + 1;
}
if (cellValue == "&日冻结-曲线数据")
{
startRow = startRow + 1;
}
// 寻找结束行
for (int nextRowIndex = rowIndex + 1; nextRowIndex <= sheet.LastRowNum; nextRowIndex++)
{
IRow nextRow = sheet.GetRow(nextRowIndex);
if (nextRow != null)
{

ICell nextCell = nextRow.GetCell(0);

if (nextCell != null)
{
if (nextCell.IsMergedCell)
{
endRow = nextRowIndex + 2;
}
else
{
string nextCellValue = nextCell.CellType == CellType.Numeric ? nextCell.NumericCellValue.ToString() : nextCell.StringCellValue;
if (nextCellValue != "*")
{
endRow = nextRowIndex + 1;
}
else
{
break;
}
}

}
}
}

sheetNames = sheet.SheetName;
model.biaoji = sheetNames;
model.isfirst = "1";
model.id = BasePage.GetNewId();
model.start_index = startRow;
model.end_index = endRow - 1;
Console.WriteLine("起始行:" + startRow);
Console.WriteLine("结束行:" + endRow);
bll_biaoji_excel.Add(model);
}

 

}
}

 

}
}
}

return true;
}

标签:Execl,cell,表格,startRow,导入,CellType,model,cellValue,endRow
From: https://www.cnblogs.com/buzhilichou/p/17719626.html

相关文章

  • el-table表格列宽度可拖拽及注意事项
    一、场景实现表格列宽度可拖拽二、方案查阅文档el-table增加border属性el-table-colunm增加resizeable属性即可实现三、问题1.如表格中有固定列,拖拽折行会导致错位解决方案:监听鼠标事件,通过doLayout重绘表格this.$nextTick(()=>{constheaderD......
  • 利用sqoop将hive数据导入导出数据到mysql
    运行环境 centos5.6  hadoop hivesqoop是让hadoop技术支持的clouder公司开发的一个在关系数据库和hdfs,hive之间数据导入导出的一个工具在使用过程中可能遇到的问题:sqoop依赖zookeeper,所以必须配置ZOOKEEPER_HOME到环境变量中。sqoop-1.2.0-CDH3B4依赖hadoop-core-0.20.2-......
  • hive导出到mysql以及mysql导入到hive
    hive导出到mysql:/export/server/sqoop-1.4.7.bin__hadoop-2.6.0/bin/sqoopexport--connectjdbc:mysql://10.99.118.207:3306/db_msg--usernameroot--password1003392478--tabletb_rs_sender_phone--export-dir/user/hive/warehouse/db_msg.db/tb_rs_sender_phone--......
  • Spring Batch数据导入导出深入探讨
    介绍SpringBatch是一个轻量级的批处理框架,它提供了一种简单的方式来处理大量数据。在本文中,我们将深入探讨SpringBatch的数据导入导出功能。数据导入SpringBatch提供了多种方式来导入数据,包括从文件、数据库、Web服务等。下面我们将分别介绍这些方式。从文件导入SpringBat......
  • Kibana中的数据导入与清洗技巧
    前言Kibana是一个非常强大的数据可视化工具,但是在使用Kibana之前,我们需要将数据导入到Elasticsearch中,并进行清洗和预处理。本文将介绍如何使用Logstash和Elasticsearch进行数据导入和清洗。LogstashLogstash是一个开源的数据收集引擎,它可以从各种来源收集数据,并将数据转换为Ela......
  • IDEA想要导入servletjar包却找不到
    在创建项目的时候,把web、tomcat都配置好后,在类中想要extendsHttpServlet却爆红找不到import的包 先打开file-->projectstructure-->Libraries 找到tomcat,lib下的servlet-api即可 ......
  • oracle导入导出bat脚本
    @echooff&setlocalenabledelayedexpansioncolor0a:startsetvar=D:\oracle\product\10.1.0\Db_1\BINsetusername=addranalysesetpassword=jt888settable=testset/pchoice=1.备份2.导入3.按任意键退出:if%choice%==1gotoexpif%choice%==2gotoimp......
  • 表格的自定义排序 编辑 拖拽 缩放
    终于能闲下来做点自己想做的事情了.. 简单表格排序  可以双击编辑自定义编辑后的规则 可拖动列进行列替换 可推动边框进行列宽度的缩放  ie6下中文不自动换行 非ie下字母和数字也不自动换行确实让人恼火 chrome浏览器下点击运行好像问题很大 拿到本地测试会比较好<!......
  • [异常处理] 下载execl 模版功能,execl 文件打不开
    问题定位:execl文件被压缩,导致打不开解决方案:pom文件中添加上:<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId......
  • KingbaseES数据库导入数据invalid byte sequence for encoding
    一、适用版本:KingbaseES数据库所有版本。二、问题现象:使用备份的数据进行还原,还原过程中发生异常。日志信息:sys_restore:connectingtodatabaseforrestoresys_restore:creatingTABLE"public.table_name"sys_restore:creatingCOMMENT"public.COLUMNtable_name.co......