2
I have a Mysql database and I want to take the data from an excel spreadsheet and send it to him.
I already know how to save the data in the table, just need to know how to scan the spreadsheet and get the data.
2
I have a Mysql database and I want to take the data from an excel spreadsheet and send it to him.
I already know how to save the data in the table, just need to know how to scan the spreadsheet and get the data.
3
I recommend using the Spreadsheetlight.
It is very simple to use and rasoably well documented.
Just download the source on the official website and attach in your solution - unfortunately there is no official Nuget package for it.
Follows the exeplo of how to modify an existing spreadsheet:
class Program
{
static void Main(string[] args)
{
// SpreadsheetLight works on the idea of a currently selected worksheet.
// If no worksheet name is provided on opening an existing spreadsheet,
// the first available worksheet is selected.
SLDocument sl = new SLDocument("ModifyExistingSpreadsheetOriginal.xlsx", "Sheet2");
sl.SetCellValue("E6", "Let's party!!!!111!!!1");
sl.SelectWorksheet("Sheet3");
sl.SetCellValue("E6", "Before anyone calls the popo!");
sl.AddWorksheet("DanceFloor");
sl.SetCellValue("B4", "Who let the dogs out?");
sl.SetCellValue("B5", "Woof!");
sl.SaveAs("ModifyExistingSpreadsheetModified.xlsx");
Console.WriteLine("End of program");
Console.ReadLine();
}
}
On their website there several examples how to perform several other tasks.
3
Open excel and press Alt+F11 to enter the platform to program in VBA.
Create an ODBC connection to connect to the database.
To loop. you can do as follows
Create a module and use the code below
Sub CadastraBD()
Dim Planilha As Worksheet
Set Planilha = Plan1 ' Plan1 é a Planilha/Worksheet
'VARRE DA LINHA1 A ULTIMA LINHA
For x = 1 To UltimaLinha(Planilha, 1)
ColunaA = Range("A" & x)
ColunaB = Range("B" & x)
ColunaC = Range("C" & x)
ColunaD = Range("D" & x)
ColunaE = Range("E" & x)
Next
End Sub
Public Function UltimaLinha(PLAN As Worksheet, COLUNA As Integer)
UltimaLinha = PLAN.Cells(65000, COLUNA).End(xlUp).Row
End Function
thanks for the tip
2
There is a basic way to do this reading of Excel spreadsheets (*.xls and *.xlsx) but, the provider
must be installed on the machine to work. In this example of two ConnectionString
one for xls
and another to xlsx
, depending on the provider
can change some variables of this ConnectionString
.
Spreadsheet Example:
Code for reading this spreadsheet
using System.Data.OleDB;
//String de Conexao
string ConnectionString = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\Temp\NamesV1.xls';Extended Properties=Excel 8.0;";
//string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\Temp\Names.xlsx;Extended Properties=\""Excel 12.0 Xml; HDR = YES""\";
//Código
using (OleDbConnection db = new OleDbConnection(ConnectionString))
using (OleDbCommand command = db.CreateCommand())
{
db.Open();
command.CommandType = System.Data.CommandType.Text;
command.CommandText = "SELECT * FROM [Sheet1$]";
using (OleDbDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while(reader.Read())
{
System.Console.WriteLine("{0} {1}", reader["Id"], reader["Name"]);
}
}
}
db.Close();
}
References:
thanks, it worked fine without giving problems. thanks
Browser other questions tagged c# excel visual-studio
You are not signed in. Login or sign up in order to post.
I will search yes, thanks. however, because Visual Studio is from microsoft, I thought it would have its own code without needing external programs.
– Italo Rodrigo
You can, but it’s a bit boring to manage. Spreadsheets, documents and presentations today are all in the same format, the Office Open XML. So to distinguish each one - saved by the file extension - it’s pretty boring. But they’re all just compressed XML files. You can create a routine to unzip the files and then work with Xmldocument, but it will not be a very pleasant experience.
– Thiago Lunardi
I used the Virginio code (which does not need external programs) and it worked well. even so, I am grateful for the dedicated time
– Italo Rodrigo