C#导入程序
来源:互联网 发布:为什么四川美女多 知乎 编辑:程序博客网 时间:2024/05/22 08:10
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using Aspose.Cells;using AsposeExcel;using DB = DBUtility.Data.DbHelper;namespace AsposeExcel{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } DataTable DT = new DataTable(); private void btnOpen_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { DataTable dt = new DataTable(); dt = ExcelToDatatalbe(openFileDialog1.FileName); dataGridView1.DataSource = dt; DT = dt; } } public DataTable ExcelToDatatalbe(string filename)//导入 { Workbook book = new Workbook(); book.Open(filename); Worksheet sheet = book.Worksheets[0]; Cells cells = sheet.Cells; //获取excel中的数据保存到一个datatable中 DataTable dt_Import = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow+1 , cells.MaxDataColumn+1 , true); // dt_Import. return dt_Import; } private void btnIn_Click(object sender, EventArgs e) { if (DT.Rows.Count <= 0) { MessageBox.Show("请选择文件", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Hand); } else { List<string> list = new List<string>() { }; string sql = ""; // string DataColumns = ""; string DataValues = ""; //string OgInPlanNo = ""; //string ProjectName = ""; string SaleNo = ""; string Cell10=""; string Cell11="" ; double Cell12 ; for (int i = 0; i < DT.Rows.Count; i++) { // DataColumns = ""; DataValues = ""; for (int j = 0; j < DT.Columns.Count; j++) { //if (DataColumns == "") // DataColumns += DT.Columns[j].ColumnName; //else // DataColumns += "," + DT.Columns[j].ColumnName;\ if (DataValues == "") { DataValues += "'" + DT.Rows[i][DT.Columns[j].ColumnName] + "'"; } else { if (j==4||j == 10 || j == 11 || j == 12 || j == 13 || j == 14||j==16||j==17) { if (j == 4) { if (DT.Rows[i][DT.Columns[j].ColumnName].ToString() == "") { DataValues += "," + "NULL"; } else { DataValues += "," + "'" + DT.Rows[i][DT.Columns[j].ColumnName] + "'"; } } else if (j == 10) { Cell10 = DT.Rows[i][DT.Columns[j].ColumnName].ToString(); DataValues += "," + "'" + Cell10 + "'"; } else if (j == 11) { Cell11 = DT.Rows[i][DT.Columns[j].ColumnName].ToString(); DataValues += "," + "'" + Cell11 + "'"; } else if (j == 12) { Cell12 = Convert.ToDouble(Cell11) - Convert.ToDouble(Cell10); DataValues += "," + "'" + Cell12 + "'"; } else if (DT.Rows[i][DT.Columns[j].ColumnName].ToString() == "") { object Number = DT.Rows[i][DT.Columns[j].ColumnName]; Number = 0; DataValues += "," + "'" + Number + "'"; } else { DataValues += "," + "'" + DT.Rows[i][DT.Columns[j].ColumnName] + "'"; } } else { DataValues += "," + "'" + DT.Rows[i][DT.Columns[j].ColumnName] + "'"; } } if (j == 7) { SaleNo = DT.Rows[i][DT.Columns[j].ColumnName].ToString(); } } Cell10 = ""; Cell11 = ""; sql = "INSERT INTO OutReport_Out (OgInPlanNo,ProjectName,CusCheck,TaxCheck,TaxTime,PickPlace,PickTime,SaleNo,TransportNo,CusName,PickWeight,EndWeight,ProfitLose,SinglePrice,SumPrice,Scale,C3Amount,C4Amount,InTime,OutTime,Rem) VALUES (" + DataValues + ")"; list.Add(sql); } string SelectSql = "select *from OutReport_Out Where SaleNo='" + SaleNo + "' "; DataSet ls= DB.GetDAO(DB.DbTypes.SqlServer, Class1.sqlcon1).Query(SelectSql); if (ls.Tables[0].Rows.Count > 0) { MessageBox.Show("重复导入数据,请删除重复数据后导入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { int count = DB.GetDAO(DB.DbTypes.SqlServer, Class1.sqlcon1).ExecuteSqlTran(list); if (list.Count > count) { MessageBox.Show("导入失败!请检查单元格格式!例如:数字列不能为空,填0;日期不能是数字", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("导入成功!请回网站页面刷新后查看!", "提示:", MessageBoxButtons.OK); } } } } private void btnDelete_Click(object sender, EventArgs e) { List<string> list = new List<string>() { }; string SaleNo = ""; for (int i = 0; i < DT.Rows.Count; i++) { for (int j = 0; j < DT.Columns.Count; j++) { if (j == 7) { SaleNo = DT.Rows[i][DT.Columns[j].ColumnName].ToString(); } } string SelectSql = " DELETE FROM OutReport_Out Where SaleNo='" + SaleNo + "' "; list.Add(SelectSql); } int l = DB.GetDAO(DB.DbTypes.SqlServer, Class1.sqlcon1).ExecuteSqlTran(list); if (list.Count > 0) { if (list.Count <= l) { MessageBox.Show("删除成功!请重新导入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show("删除失败,请先导入文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } else { MessageBox.Show("删除失败,请先导入文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } }}
0 0
- C#导入程序
- Excel导入程序 C#
- C# 程序导入导出oracle数据库
- c# 程序实现ACCESS数据导入SQL的方法
- C# winform程序中导入excel的一些处理
- C# 导入word word导入
- 在C#中导入WebBrowser控件,生成具有强名称的程序集
- c# Winform程序实现多sheet的Excel文件导入与导出
- 用c#窗体应用游戏程序导入图片资源及用数组存储
- EXCAL导入数据库 C#
- C#导入excel2003数据表
- C#中EXCEL导入
- c#注册表导出导入
- C#导入excel重写
- Excel 导入sqlserver c#
- c#导入导出Excel
- C# 导入Excel
- C# excel数据导入
- 跟我一起云计算(2)——YARN
- Python 中的缺失值及其处理
- 基于Redis+MySQL+MongoDB存储架构应用
- 几种常用加密算法比较
- spark Runtime内幕解密
- C#导入程序
- STL queue的使用
- java发送邮件
- 大数据框架对比:Hadoop、Storm、Samza、Spark和Flink
- JHipster-- 初次使用之创建实体
- krpano全景之移动手机端默认加载开启陀螺仪
- angular route $state.go('hospital_doctors', { hospital_id: hospital_id })传值页面刷新怎么保存
- 相关子查询
- JS实现系统时间(自动)