嗨,我有包含以下字段名称的 excel 文件,mobileNo,TotalCoupen。我想在 datagridview 中使用唯一序列号导入这些字段。如果一个人总共有 5 个 coupen,它将显示 5 个 coupen 序列,如 (10001,10002,10003,10004, 10005)我还附上了图片
这是我的代码,此代码成功加载 excel 文件,但不生成 coupen 不,它仅导入 excel 文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;
namespace ReadExcelFileApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}
private void btnChoose_Click(object sender, EventArgs e)
{
string filePath = string.Empty;
string fileExt = string.Empty;
OpenFileDialog file = new OpenFileDialog();//open dialog to choose file
if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK)//if there is a file choosen by the user
{
filePath = file.FileName;//get the path of the file
fileExt = Path.GetExtension(filePath);//get the file extension
if (fileExt.CompareTo(".xls") == 0 || fileExt.CompareTo(".xlsx") == 0)
{
try
{
DataTable dtExcel = new DataTable();
dtExcel = ReadExcel(filePath, fileExt);//read excel file
dataGridView1.Visible = true;
dataGridView1.DataSource = dtExcel;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
else
{
MessageBox.Show("Please choose .xls or .xlsx file only.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);//custom messageBox to show error
}
}
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();//to close the window(Form1)
}
public DataTable ReadExcel(string fileName, string fileExt)
{
string conn = string.Empty;
DataTable dtexcel = new DataTable();
if (fileExt.CompareTo(".xls") == 0)//compare the extension of the file
conn = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties='Excel 8.0;HRD=Yes;IMEX=1';";//for below excel 2007
else
conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';";//for above excel 2007
using (OleDbConnection con = new OleDbConnection(conn))
{
try
{
OleDbDataAdapter oleAdpt = new OleDbDataAdapter("select * from [Sheet1$]", con);//here we read data from sheet1
oleAdpt.Fill(dtexcel);//fill excel data into dataTable
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
return dtexcel;
}
}
}
我不确定您为什么要添加这些恕我直言重复的行。一个简单的解决方案是DataTable
用额外的行创建一个新的。循环遍历 excel 数据表中的所有行,然后total coupen
为每个新行循环时间,然后如图所示更新coupen no
。同样,我不确定您为什么要这样做,但这是一种方法。下面的代码DataTable
从DataTable
返回的ReadExcel
方法中创建一个新的。这些AddDuplicates
方法根据要求添加行。
dtExcel = ReadExcel(filePath, fileExt);//read excel file
DataTable dgvTable = AddDuplicates(dtExcel);
dataGridView1.Visible = true;
//dataGridView1.DataSource = dtExcel;
dataGridView1.DataSource = dgvTable;
private DataTable AddDuplicates(DataTable dt) {
DataTable dtcopy = dt.Clone();
int curCount = 100000;
double coupenCount = 0;
foreach(DataRow dr in dt.Rows) {
coupenCount = (double)dr.ItemArray[2];
for (int i = 0; i < coupenCount; i++) {
dtcopy.Rows.Add(dr.ItemArray[0], dr.ItemArray[1], ++curCount);
}
}
return dtcopy;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句