I want to import sing column of excel sheet into database table name admin_record and excel sheet is only consist of one column and name of the column is bar codes and rows can be many.
I want to save it in database my table name is admin_record and its consists of only two fields
id (which is primary key)
bar_code.
find only this code which i totally cant understand
public function read_file($table = 'admin_record', $filename = 'example.xls') {
$pathToFile = 'uploads/' . $filename;
$this->load->library('Spreadsheet_Excel_Reader');
$data = new Spreadsheet_Excel_Reader($pathToFile);
$sql = "INSERT INTO $table (";
for($index = 1;$index <= $data->sheets[0]['numCols']; $index++){
$sql.= strtolower($data->sheets[0]['cells'][1][$index]) . ", ";
}
$sql = rtrim($sql, ", ")." ) VALUES ( ";
for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
$valuesSQL = '';
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
$valuesSql .= "\"" . $data->sheets[0]['cells'][$i][$j] . "\", ";
}
echo $sql . rtrim($valuesSql, ", ")." ) <br>";
}
}
your library file is wrong please use following link to download
Spreadsheet_Excel_Reader Library
public function my_test($table = 'admin_record', $filename = 'resources/uploads/c.xls')
{
$pathToFile = $filename;
$valuesSql="";
$this->load->library('Spreadsheet_Excel_Reader');
$data = new Spreadsheet_Excel_Reader($pathToFile);
$sql = "INSERT INTO $table (";
for($index = 1;$index <= $data->sheets[0]['numCols']; $index++){
$sql.= strtolower($data->sheets[0]['cells'][1][$index]) . ", ";
}
$sql = rtrim($sql, ", ")." ) VALUES ( ";
for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) {
$valuesSQL = '';
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
$valuesSql .= "\"" . $data->sheets[0]['cells'][$i][$j] . "\", ";
}
echo $sql . rtrim($valuesSql, ", ")." ) <br>";
} // add this line
}
Related
I have this code:
$passwordsParameters = "sysPassword = 12 &&& testPass = 13 &&& systemPassword = 10"
$parametersList = #($passwordsParameters -split '&&&')
$passwordsTable = #{}
ForEach ($parameter in $parametersList) {
$splitToKeyValue = #($parameter -split '=')
$passwordsTable += $passwordsTable = #{
$splitToKeyValue[0].trim() = $splitToKeyValue[1].trim()
}
}
ForEach ($pass in $passwordsTable.Keys) {
if ($passwordsTable[$pass] -ne "") {
Write-Host "set $pass ="$passwordsTable[$pass]""
} else {
Write-Host "A value for the parameter $pass was not entered."
}
}
# Add-Content "d:\myFile.txt" "set $pass ="$passwordsTable[$pass]""
Which perfectly works when I use Write-Host. But I want to do something like in the comment in line 25. I tried several ways but I always got a static string instead of the values that I get from the Hashtable.
At the end I want to have something like:
set pass1 = 12
set pass2 = 5
in myFile.txt
Any help is appreciated. Thanks!
You could change Write-Host (just prints to a console) to Write-Output ( which passes an object to a pipeline). Write-Output does not print to the console.
$passwordsParameters = "sysPassword = 12 &&& testPass = 13 &&& systemPassword = 10"
$parametersList = #($passwordsParameters -split '&&&')
$passwordsTable = #{}
ForEach ($parameter in $parametersList) {
$splitToKeyValue = #($parameter -split '=')
$passwordsTable += $passwordsTable = #{
$splitToKeyValue[0].trim() = $splitToKeyValue[1].trim()
}
}
$counter=0
ForEach ($pass in $passwordsTable.Keys) {
if ($passwordsTable[$pass] -ne "") {
$counter++
Write-Output "set "pass$counter = $passwordsTable[$pass]"`n" | Add-Content -NoNewline myFile.txt
} else {
Write-Host "A value for the parameter $pass was not entered."
}
}
Output:
set pass1=10
set pass2=13
set pass3=12
You can replace the first foreach loop if you simply replace all the &&& by a newline and use cmdlet ConvertFrom-StringData.
Add-Content also has a switch called -PassThru that will let you write to the file and also output to console.
$passwordsParameters = "sysPassword = 12 &&& testPass = 13 &&& systemPassword = 10"
$passwordsTable = $passwordsParameters -replace '&&&', [Environment]::NewLine | ConvertFrom-StringData
foreach ($pass in $passwordsTable.Keys) {
if ($passwordsTable[$pass]) {
$msg = 'set {0} = {1}' -f $pass, $passwordsTable[$pass]
# or use: $msg = "set $pass = $($passwordsTable[$pass])"
# write this to the file. switch -PassThru will also output to the console
Add-Content -Path 'D:\myFile.txt' -Value $msg -PassThru
} else {
Write-Host "A value for the parameter '$pass' was not entered."
}
}
I try to write a script that able to read from excel (line by line for specific columns) and retrieve the value. Eg. Column A and Column C. After searching for days, I only able to found simple script that able to read all from excel and print it out.
In Excel
Column A Column B Column C
Dog Cat PIG
Elephant Rat Snake
My code for now.
use strict;
use Spreadsheet::ParseXLSX;
use Spreadsheet::WriteExcel;
my $parser = Spreadsheet::ParseXLSX->new();
my $workbook = $parser->parse('C:\Modules\list.xlsx');
if ( !defined $workbook ) {
die $parser->error(), ".\n";
}
for my $worksheet ( $workbook->worksheets() ) {
my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();
for my $row ( $row_min .. $row_max ) {
for my $col ( $col_min .. $col_max ) {
my $cell = $worksheet->get_cell( $row, $col );
next unless $cell;
print "Row, Col = ($row, $col)\n";
print "Value = ", $cell->value(), "\n";
print "Unformatted = ", $cell->unformatted(), "\n";
print "\n";
}
}
}
Expected Result: Able to retrieve column A and column C values and turn it into variable then I will use the variable for other purpose(will use it on a looping statement).
Retrieve value from excel (Below is my concept how I want to do it)
#Loop Start (loop through excel line by line)
$VariableA = Dog(from column A); or ($VariableA =elephant(from column A));
**select from database**
$sql = "select * from animal where name='$VariableA'";
**perform $sql and other functions**
#End Loop
Thanks for helping, anything u can share to me? Thanks!
Try the below code and as per your requirement i have stored the values in colA and colC as shown below:
Code:
use strict;
use warnings;
use Spreadsheet::ParseXLSX;
use Spreadsheet::WriteExcel;
my $parser = Spreadsheet::ParseXLSX->new();
my $workbook = $parser->parse('C:\Modules\list.xlsx');
my #columnA = ();
my #columnC = ();
if ( !defined $workbook ) {
die $parser->error(), ".\n";
}
#Get cell value from excel sheet1 row 1 column 2
my $worksheet = $workbook->worksheet('Sheet1');
my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();
for my $row ( $row_min .. $row_max ) {
my $cellA = $worksheet->get_cell( $row, 0 );
# Print the cell value when not blank
if ( defined $cellA and $cellA->value() ne "" ) {
my $value = $cellA->value();
push( #columnA, $value );
}
my $cellC = $worksheet->get_cell( $row, 2 );
if ( defined $cellC and $cellC->value() ne "" ) {
my $value = $cellC->value();
push( #columnC, $value );
}
}
print "First Value of ColumnA is retrieved:";
my $colA = pop(#columnA);
print $colA;
print "\nFirst Value of ColumnC is retrived:";
my $colC = pop(#columnC);
print $colC;
I have in my PHP file this string :
$sql = "SELECT p.pac_rut a, ";
$sql .= " p.pac_dig b, ";
$sql .= " p.pac_apepat c, ";
$sql .= " p.pac_apemat d, ";
$sql .= " p.pac_nombre e, ";
$sql .= " to_char(p.pac_fecha_nac,'dd/mm/yyyy') as f, ";
$sql .= " (to_char(sysdate,'yyyy')-to_char(p.pac_fecha_nac,'yyyy')) as g, ";
$sql .= " p.sex_cod h ";
$sql .= "FROM mytable ";
$sql .= "WHERE p.pac_rut=$rut";
And sed ':a;N;$!ba;s/\n//g' | sed 's/"; $sql .= "//g' | sed 's/ //g' | sed 's/=\$/=:/g' gives me the result ok like : $sql= "SELECT p.pac_rut a,p.pac_dig b,p.pac_apepat c,p.pac_apemat d,p.pac_nombre e,to_char(p.pac_fecha_nac,'dd/mm/yyyy') as f,(to_char(sysdate,'yyyy')-to_char(p.pac_fecha_nac,'yyyy')) as g,p.sex_cod h FROM salud.pacientes p WHERE p.pac_rut=:rut";
But if a have 2 (or more) blocks of sql queries, i need a function which can build 2 (or more) queries sql on 2 (or more) lines.
For ejample, i have :
$sql = "SELECT p.pac_rut a ";
$sql .= "FROM mytable ";
$sql .= "WHERE p.pac_rut=$rut";
and
$sql2 = "SELECT p.pac_rut a ";
$sql2 .= "FROM mytable ";
$sql2 .= "WHERE p.pac_rut=$rut";
in the same file, i need this result :
$sql = "SELECT p.pac_rut a FROM mytable WHERE p.pac_rut=$rut";
$sql2 = "SELECT p.pac_rut a FROM mytable WHERE p.pac_rut=$rut";
How can i do that with awk or sed or other linux tool ?
I've been trying to execute the following code to pull information out of an Excel spreadsheet but it keeps failing with the following error:
Method invocation failed because [System.__ComObject#{000208d8-0000-0000-c000-000000000046}] doesn't contain a method named 'cells'.
At run.ps1:42 char:52
+ for ($row = [int]$matches[1] + 1; $sh.cells <<<< ($row, 2) -ne ""; $row++)
+ CategoryInfo : InvalidOperation: (cells:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Any idea how I can resolve this? Here is my code:
$(for ($i=3; $i -le $wb.sheets.count; $i++){
$sh=$wb.Sheets.Item($i)
$startCell_appname = $sh.cells.item(1,2).EntireColumn.find("Application Name").Address()
if ($startCell_appname -match '\$\w+\$(\d+)')
{
for ($row = [int]$matches[1] + 1; $sh.Cells($row, 2) -ne ""; $row++)
{
$apps = "" | Select appname,location,lob,os
$apps.appname = $sh.Cells($row, 2).Value2
# I don't know if the location value is in column 3; this is just an example.
$apps.location = $sh.Cells($row, 3).Value2
$apps.lob = $sh.Name
$apps.os = "Windows 7"
$apps
}
}
}) | Export-csv "apps.csv" -NoTypeInformation
Use $sh.Cells.Item($row,2) like you do where you are assigning to $startCell_appname.
I'm having issues with my Powershell script - its meant to loop through a series of workbooks but it freezes after the first one. Any idea why this could be happening?
$(for ($i=3; $i -le $wb.sheets.count; $i++){
$sh=$wb.Sheets.Item($i)
$startCell = $sh.cells.item(1,2).EntireColumn.find("Application Name").Address()
if($startCell -match '\$\w+\$(\d+)')
{
for($row=[int]$matches[1]+1; $sh.cells.item($row,2) -ne ""; $row++)
{
$apps = "" | Select appname,location,lob
$apps.appname = $sh.Cells($row, 2).Value2
$apps.location = $sh.Cells($row, 10).Value2
$apps.lob = $sh.Name
$apps
}
}
})