Buenas, estoy realizando un proyecto en windows form y lo que hace es subir un archivo excel pasar los datos a un dataGridView y guardarlos en una base de datos MySQL, el problema es que este excel muestra datos de un ciclo de 4 meses y este es el ultimo mes por ende muestra FEBRERO, MARZO, ABRIL Y MAYO el próximo mes mostrará JUNIO, JULIO, AGOSTO Y SEPTIEMBRE no se como hacer el cambio del nombre de los meses según corresponda a cada periodo, pensé en crear la base de datos con los 12 meses e ir llenando según el header del dataGridView pero no se como puedo guardarlo en una variable o algo.
Dejo el código para que me puedan ayudar.
ESTA ES LA CLASE IMPORTAEXCEL
class importarExcel
{
OleDbConnection conn;
OleDbDataAdapter MyDataAdapter;
DataTable dt;
public void importarExcel(DataGridView dgv, String nombreHoja, string ruta)
{
try
{
conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;data source=" + ruta + ";Extended Properties='Excel 12.0 Xml;HDR=Yes'");
MyDataAdapter = new OleDbDataAdapter("Select * from [" + nombreHoja + "$]", conn);
dt = new DataTable();
MyDataAdapter.Fill(dt);
dgv.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show("La ruta y/o el Nombre de la hoja son incorrectos");
}
}
ESTE ES EL CÓDIGO DEL BOTÓN PARA BUSCAR EL ARCHIVO
string ruta = "";
OpenFileDialog openfile1 = new OpenFileDialog();
openfile1.Filter = "Excel Files |*.xlsx";
openfile1.Title = "Seleccione el archivo de Excel";
if (openfile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (openfile1.FileName.Equals("") == false)
{
ruta = openfile1.FileName;
textBox1.Text = ruta;
textBox2.Text = "";
textBox2.Focus();
textBox1.Enabled = false;
}
}
ESTE EL CÓDIGO DEL BOTÓN PARA IMPORTAR LOS DATOS AL DATAGRIDVIEW
string nom = textBox2.Text;
string ruta = textBox1.Text;
new importar().importarExcel(dataGridView1, nom, ruta);
Y ESTE EL CÓDIGO DEL BOTÓN PARA SUBIRLO A LA BD
foreach (DataGridViewRow row in dataGridView1.Rows)//CICLO PARA RECORRER LA TABLA
{
a1 = Convert.ToString(row.Cells["Sold-to party"].Value);
a2 = Convert.ToString(row.Cells["Ship-to-party"].Value);
a3 = Convert.ToString(row.Cells["Cliente"].Value);
a4 = Convert.ToString(row.Cells["Cotización"].Value);
a5 = Convert.ToString(row.Cells["Material"].Value);
a6 = Convert.ToString(row.Cells["Descripción"].Value);
a7 = Convert.ToString(row.Cells["Febrero"].Value);
a8 = Convert.ToString(row.Cells["Marzo"].Value);
a9 = Convert.ToString(row.Cells["Abril"].Value);
a10 = Convert.ToString(row.Cells["Mayo"].Value);
a11 = Convert.ToString(row.Cells["Total Cotizado"].Value);
a12 = Convert.ToString(row.Cells["PO Number"].Value);
a13 = Convert.ToString(row.Cells["Cantidad Cotización"].Value);
a14 = Convert.ToString(row.Cells["Cantidad OC"].Value);
a15 = Convert.ToString(row.Cells["Valor OC"].Value);
a16 = Convert.ToString(row.Cells["Éxito"].Value);
a17 = Convert.ToString(row.Cells["SBU"].Value);
string estado="";//VARIBLE HAY QUE PROGRAMAR EL ESTADO
con.ingresarcotizaciones(a1, a2, a3, a4, a5, a6, int.Parse(a7), int.Parse(a8), int.Parse(a9), int.Parse(a10), int.Parse(a11), a12, int.Parse(a13), int.Parse(a14), int.Parse(a15), int.Parse(a16), int.Parse(a17), estado);
Espero puedan ayudarme.