How to execute query on csv file - c#-4.0

i have uploaded a csv file and also make a data-table from csv file.now i want to find distinct values from each columns of data-table.what will be the code for this.
i have tried by this code but there is error show that object is not ADODB.what is ADODB and how can i remove this error
public static DataTable GetDataTabletFromCSVFile(string csv_file_path)
{
DataTable csvData = new DataTable();
try
{
using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
//read column names
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
}
csvData.Rows.Add(fieldData);
}
foreach (DataColumn col in csvdata.Columns)
{
foreach (DataRow ro in csvdata.Rows)
{
textBox1.Text = "" + ro[col];
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Related

Apache POI - remove external Book reference

We receive excel files in our system and upon downloading them, i need to remove any reference to external workbook for security reasons.
i wrote the following function to remove external links as well as removing content from the cell referencing the book.
public static byte[] cleanFile(byte[] excelByteArrayworkbook) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (InputStream targetStream = new ByteArrayInputStream(excelByteArrayworkbook)){
try (XSSFWorkbook workbook = new XSSFWorkbook(targetStream)) {
XSSFEvaluationWorkbook evalWorkbook = XSSFEvaluationWorkbook.create((XSSFWorkbook) workbook);
Iterator<Sheet> sheetIterator = workbook.iterator();
int i = 0;
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
EvaluationSheet evalSheet = evalWorkbook.getSheet(i);
i++;
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
EvaluationCell evaluationCell = evalSheet.getCell(cell.getRowIndex(), cell.getColumnIndex());
try {
Ptg[] formulaTokens = evalWorkbook.getFormulaTokens(evaluationCell);
for (Ptg formulaToken : formulaTokens) {
int externalSheetIndex = -1;
if (formulaToken instanceof Ref3DPtg) {
Ref3DPtg refToken = (Ref3DPtg) formulaToken;
externalSheetIndex = refToken.getExternSheetIndex();
} else if (formulaToken instanceof Area3DPtg) {
Area3DPtg refToken = (Area3DPtg) formulaToken;
externalSheetIndex = refToken.getExternSheetIndex();
} else if (formulaToken instanceof Ref3DPxg) {
Ref3DPxg refToken = (Ref3DPxg) formulaToken;
externalSheetIndex = refToken.getExternalWorkbookNumber();
} else if (formulaToken instanceof Area3DPxg) {
Area3DPxg refToken = (Area3DPxg) formulaToken;
externalSheetIndex = refToken.getExternalWorkbookNumber();
}
if (externalSheetIndex >= 0) {
cell.setCellFormula(null);
}
else if (cell.getCellFormula().contains("[")) {
cell.setCellFormula(null);
}
}
} catch (Exception e) {
cell.setCellFormula(null);
}
}
}
}
}
List<ExternalLinksTable> links = workbook.getExternalLinksTable();
links.forEach(link -> {
if(link.getCTExternalLink().isSetDdeLink())
link.getCTExternalLink().unsetDdeLink();
if(link.getCTExternalLink().isSetExtLst())
link.getCTExternalLink().unsetExtLst();
if(link.getCTExternalLink().isSetOleLink())
link.getCTExternalLink().unsetOleLink();
if(link.getCTExternalLink().isSetExternalBook())
link.getCTExternalLink().unsetExternalBook();
link.getCTExternalLink().setNil();
});
try {
workbook.write(bos);
} finally {
bos.close();
}
}
}
try {
bos.close();
} catch (IOException e) {
}
return bos.toByteArray();
}
However, the file i am getting is not valid. Excel prompt me with an error message saying
Excel error message
The version of Apache POI i am using is 3.13 . I can't find an example where i can remove external links
I found the answer. The code was not removing references in the Name Manager area.
I have added the following in my function
List<XSSFName> nameUsingExternalBook = new ArrayList<XSSFName>();
for (int j = 0; j < workbook.getNumberOfNames(); j++) {
XSSFName name= workbook.getNameAt(j);
if(name.getRefersToFormula() != null && name.getRefersToFormula().contains("[")) {
nameUsingExternalBook.add(name);
}
}
nameUsingExternalBook.forEach(name->{
workbook.removeName(name.getNameName());
});
So the new excel file is wellformed

Using Epplus to import data from an Excel file to SQL Server database table

I've tried implementing thishttps://www.paragon-inc.com/resources/blogs-posts/easy_excel_interaction_pt6 on an ASP.NET MVC 5 Application.
//SEE CODE BELOW
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
var regPIN = DB.AspNetUsers.Where(i => i.Id == user.Id).Select(i => i.registrationPIN).FirstOrDefault();
if (file != null && file.ContentLength > 0)
{
var extension = Path.GetExtension(file.FileName);
var excelFile = Path.Combine(Server.MapPath("~/App_Data/BulkImports"),regPIN + extension);
if (System.IO.File.Exists(excelFile))
{
System.IO.File.Delete(excelFile);
}
else if (file.ContentType == "application/vnd.ms-excel" || file.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
file.SaveAs(excelFile);//WORKS FINE
//BEGINING OF IMPORT
FileInfo eFile = new FileInfo(excelFile);
using (var excelPackage = new ExcelPackage(eFile))
{
if (!eFile.Name.EndsWith("xlsx"))//Return ModelState.AddModelError()
{ ModelState.AddModelError("", "Incompartible Excel Document. Please use MSExcel 2007 and Above!"); }
else
{
var worksheet = excelPackage.Workbook.Worksheets[1];
if (worksheet == null) { ModelState.AddModelError("", "Wrong Excel Format!"); }// return ImportResults.WrongFormat;
else
{
var lastRow = worksheet.Dimension.End.Row;
while (lastRow >= 1)
{
var range = worksheet.Cells[lastRow, 1, lastRow, 3];
if (range.Any(c => c.Value != null))
{ break; }
lastRow--;
}
using (var db = new BlackBox_FinaleEntities())// var db = new BlackBox_FinaleEntities())
{
for (var row = 2; row <= lastRow; row++)
{
var newPerson = new personalDetails
{
identificationType = worksheet.Cells[row, 1].Value.ToString(),
idNumber = worksheet.Cells[row, 2].Value.ToString(),
idSerial = worksheet.Cells[row, 3].Value.ToString(),
fullName = worksheet.Cells[row, 4].Value.ToString(),
dob = DateTime.Parse(worksheet.Cells[row, 5].Value.ToString()),
gender = worksheet.Cells[row, 6].Value.ToString()
};
DB.personalDetails.Add(newPerson);
try { db.SaveChanges(); }
catch (Exception) { }
}
}
}
}
}//END OF IMPORT
ViewBag.Message = "Your file was successfully uploaded.";
return RedirectToAction("Index");
}
ViewBag.Message = "Error: Your file was not uploaded. Ensure you upload an excel workbook file.";
return View();
}
else
{
ViewBag.Message = "Error: Your file was not uploaded. Ensure you upload an excel workbook file.";
return View();
}
}
See Picture Error
Any help would be greatly appreciated mates.
you can do like this:
public bool readXLS(string FilePath)
{
FileInfo existingFile = new FileInfo(FilePath);
using (ExcelPackage package = new ExcelPackage(existingFile))
{
//get the first worksheet in the workbook
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int colCount = worksheet.Dimension.End.Column; //get Column Count
int rowCount = worksheet.Dimension.End.Row; //get row count
string queryString = "INSERT INTO tableName VALUES"; //Here I am using "blind insert". You can specify the column names Blient inset is strongly not recommanded
string eachVal = "";
bool status;
for (int row = 1; row <= rowCount; row++)
{
queryString += "(";
for (int col = 1; col <= colCount; col++)
{
eachVal = worksheet.Cells[row, col].Value.ToString().Trim();
queryString += "'" + eachVal + "',";
}
queryString = queryString.Remove(queryString.Length - 1, 1); //removing last comma (,) from the string
if (row % 1000 == 0) //On every 1000 query will execute, as maximum of 1000 will be executed at a time.
{
queryString += ")";
status = this.runQuery(queryString); //executing query
if (status == false)
return status;
queryString = "INSERT INTO tableName VALUES";
}
else
{
queryString += "),";
}
}
queryString = queryString.Remove(queryString.Length - 1, 1); //removing last comma (,) from the string
status = this.runQuery(queryString); //executing query
return status;
}
}
Details: http://sforsuresh.in/read-data-excel-sheet-insert-database-table-c/

Selenium Webdriver How to select records from table by fetching Excel Input

im struggeling for below scenario.
Application displayed records of 100 suppliers in one table have three columns namely as ID,Company name and Subscription name.
i want to take input from my excel sheet say company name"xyz" and using that input i have to click on subscription name details link so application will navigates me next page.
Sample code i have created as below:
`public static void main(String[] args) throws BiffException, IOException, Exception {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
//Workbook location
Workbook wBook = Workbook.getWorkbook(new File("C:\Users\amit.bhagwat\Documents\TestData\SampleData.xls"));
//get sheet
jxl.Sheet Sheet = wBook.getSheet(0);
//loop
for(int i=1; i<Sheet.getRows(); i++)
{
driver.get("http://206.132.42.243/Web");
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[#id='UserName']")).sendKeys(Sheet.getCell(0, i).getContents());
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[#id='Password']")).sendKeys(Sheet.getCell(1, i).getContents());
driver.findElement(By.xpath("//input[#id='Password']")).sendKeys(Sheet.getCell(1, i).getContents());
Thread.sleep(40);
driver.findElement(By.xpath("//input[#name='Login']")).click();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.findElement(By.xpath("//a[contains(text(),'Task')]")).click();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.findElement(By.xpath("//a[contains(text(),'Data Checking')]")).click();
jxl.Sheet Sheet2 = wBook.getSheet(0);
WebElement kancheck = driver.findElement(By.name("Grant & Brown"));
kancheck.click();
System.out.println(kancheck.isSelected());
driver.findElement(By.xpath("//a[contains(text(),'Data Checking')]")).sendKeys(Sheet2.getCell(1, i).getContents());
Thread.sleep(40);` enter code here
As far as I could understand, you are trying to read the file from a remote location and then read the information from it. It would be a good practice if you can use Apache POI library to read contents at run-time.
In my project, I read all the contents from an excel sheet usingApache POI library to set the values of my variables. Here is a code snippet on how i achieved it. Hopefully this will guide you to a proper solution. :)
public void readExcelDoc() throws FileNotFoundException, IOException
{
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("excelDoc//scripts.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = null;
HSSFCell cell = null;
int rows = 0; // No of rows
// rows = sheet.getPhysicalNumberOfRows();
rows = sheet.getLastRowNum();
int cols = 2; // No of columns
int tmp = 0;
// This trick ensures that we get the data properly even if it doesn't start from first few rows
for(int i = 0; i < 10 || i < rows; i++) {
row = sheet.getRow(i);
if(row != null) {
tmp = sheet.getRow(i).getPhysicalNumberOfCells();
if(tmp > cols) cols = tmp;
}
}
int testRowNo = 0;
String rowName = "Test Name";
String columnValue = " ";
//Iterate through Row and columns here. Excluding 1st row for title names
for(int r = 1; r <= rows; r++) {
row = sheet.getRow(r);
if(row != null) {
//Browse through columns using c
for(int c = 0; c < cols; c++) {
if(c==0) //Only taking data from Cell 0; Ignoring any other inputs
{
cell = row.getCell((short)c);
try
{
if(cell.getStringCellValue().contains(rowName))
{
testRowNo =row.getRowNum();
}
if(testRowNo > 0 )
{
if(cell.getColumnIndex() == 0 && row.getRowNum() > testRowNo && cell.getStringCellValue().length() !=0)
{
try{
String cellValue = cell.getStringCellValue().toLowerCase();
//System.out.println(cellValue);
scriptType.add(cellValue);
}
catch(IllegalStateException e)
{
e.printStackTrace();
scriptType.add(cell.getStringCellValue());
}
}
}
}
catch(NullPointerException e)
{
}
}
if(c==1)
{
cell = row.getCell((short)c); //this sets the column number
if(testRowNo == 0)
{
try{
String cellValue = cell.getStringCellValue();
//System.out.println(cellValue);
columnValue = cellValue;
}
catch(IllegalStateException e)
{
String cellValue = cell.toString();
columnValue = cellValue;
}
catch(NullPointerException e)
{
String cellValue = nodata;
columnValue = cellValue;
}
}
}
if(c==2)
{
cell = row.getCell((short)c); //this sets the column number
if(testRowNo == 0)
{
try{
String cellValue = cell.getStringCellValue();
//System.out.println(cellValue);
inputParameters.put(cellValue, columnValue);
}
catch(IllegalStateException e)
{
String cellValue = cell.toString();
inputParameters.put(cellValue, columnValue);
}
catch(NullPointerException e)
{
String cellValue = nodata;
inputParameters.put(cellValue, columnValue);
}
}
}
}
}
}
System.out.println("---------The parameters set from excel are : ---------");
#SuppressWarnings("rawtypes")
Iterator iterator = inputParameters.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next().toString();
String value = inputParameters.get(key).toString();
System.out.println(key + " : " + value);
}
}

OleDbDataAdapter.Update returns rows, but no rows added to Excel spreadsheet

I can add rows to my Excel spreadsheet one row at a time, but it is incredibly slow (1 minute for 400 records, even using Prepare). So, I know the Sql is valid and the DataTable is good.
The code that works:
public void InsertFromDataTable(string strSql, DataTable dtTable, string strTableName)
{
if (m_oleDbHandler == null)
{
m_oleDbHandler = new OleDbHandler(m_strConnection);
}
//Do one row at a time since the DataAdapter did not work
foreach (DataRow drRow in dtTable.Rows)
{
OleDbParmCollection cololedbParameters = new OleDbParmCollection();
foreach (DataColumn dcColumn in dtTable.Columns)
{
OleDbParameter odpParameter = new OleDbParameter("#" + dcColumn.ColumnName, drRow[dcColumn.ColumnName]);
odpParameter.ParameterName = "#" + dcColumn.ColumnName;
odpParameter.DbType = OleDbHandler.GetDbType(dcColumn.GetType());
odpParameter.Size = dcColumn.MaxLength;
odpParameter.SourceColumn = dcColumn.ColumnName;
cololedbParameters.Add(odpParameter);
}
m_oleDbHandler.ExecuteCommand(strSql, cololedbParameters, true);
}
}
}
When I try to do the same thing using a DataAdapter, it says it returns 458 rows, but there are no new rows in the spreadsheet. The code that fails:
//DataAdapter version
OleDbParmCollection cololedbParameters = new OleDbParmCollection();
foreach (DataColumn dcColumn in dtTable.Columns)
{
OleDbParameter odpParameter = new OleDbParameter();
odpParameter.ParameterName = "#" + dcColumn.ColumnName;
odpParameter.OleDbType = OleDbHandler.GetOleDbType(dcColumn.GetType());
odpParameter.DbType = OleDbHandler.GetDbType(dcColumn.GetType());
odpParameter.Size = dcColumn.MaxLength;
odpParameter.SourceColumn = dcColumn.ColumnName;
cololedbParameters.Add(odpParameter);
}
m_oleDbHandler.InsertFromDataTable(strSql, dtTable, cololedbParameters, strTableName);
and then:
public int InsertFromDataTable(string strSql, DataTable dtTable, OleDbParmCollection cololeDbParameters, string strTableName)
{
//Set every row as added so that they will be inserted
foreach (DataRow drRow in dtTable.Rows)
{
drRow.SetAdded();
}
//Update the output table
int intRows = -1;
try
{
OleDbCommand oleDbCommand = new OleDbCommand(strSql, OpenConnection());
foreach (OleDbParameter oleDbParameter in cololeDbParameters)
{
if (oleDbParameter.Value == null)
{
oleDbCommand.Parameters.Add(oleDbParameter.ParameterName, OleDbType.VarChar).Value = DBNull.Value;
}
else if (string.IsNullOrEmpty(oleDbParameter.Value.ToString()))
{
oleDbCommand.Parameters.Add(oleDbParameter.ParameterName, OleDbType.VarChar).Value = DBNull.Value;
}
else
{
oleDbCommand.Parameters.Add(oleDbParameter);
}
}
OleDbDataAdapter odaAdapter = new OleDbDataAdapter(new OleDbCommand("SELECT * FROM " + strTableName, OpenConnection()));
odaAdapter.InsertCommand = oleDbCommand;
odaAdapter.MissingMappingAction = MissingMappingAction.Passthrough;
odaAdapter.MissingSchemaAction = MissingSchemaAction.Error;
odaAdapter.TableMappings.Add(strTableName, dtTable.TableName);
foreach (DataColumn dcColumn in dtTable.Columns)
{
odaAdapter.TableMappings[0].ColumnMappings.Add(dcColumn.ColumnName, dcColumn.ColumnName);
}
intRows = odaAdapter.Update(dtTable);
}
catch (OleDbException ex)
{
LogStackTrace();
LogToDb.LogException(ex, LogToDb.c_strAppError);
LogToDb.LogMessage("OleDb error", "OleDbHandler.InsertFromDataTable error", strSql, LogToDb.c_intErrorLevelOleDb);
CancelTransactionAndClose();
throw;
}
finally
{
CloseConnection();
}
return (intRows);
}
Why would I get intRows = 458, but there are no new rows in the Excel file?
EDIT: I just did a test to see what happens if I export to a Microsoft Access .mdb (instead of Excel), and the results tell me something. I get 458 blank rows. so, I suspect I am getting 458 blank rows in Excel. So, now the question is why the rows are all blank.
Got it -- the error was in the section below. This works well for an ExecuteNonQuery, but lousy when there is no data.
foreach (OleDbParameter oleDbParameter in cololeDbParameters)
{
if (oleDbParameter.Value == null)
{
oleDbCommand.Parameters.Add(oleDbParameter.ParameterName, OleDbType.VarChar).Value = DBNull.Value;
}
else if (string.IsNullOrEmpty(oleDbParameter.Value.ToString()))
{
oleDbCommand.Parameters.Add(oleDbParameter.ParameterName, OleDbType.VarChar).Value = DBNull.Value;
}
else
{
oleDbCommand.Parameters.Add(oleDbParameter);
}
}
The corrected code, which works well with both Access and Excel is:
foreach (OleDbParameter oleDbParameter in cololeDbParameters)
{
oleDbCommand.Parameters.Add(oleDbParameter);
}
There was a second, less serious error. I used
OleDbHandler.GetOleDbType(dcColumn.GetType());
which should have been
OleDbHandler.GetOleDbType(dcColumn.DataType);

how to read data from csv file into C# console Application

using System;
namespace jagged_array
{
class Program
{
static void Main(string[] args)
{
string[][] Members = new string[10][]{
new string[]{"amit","amit#gmail.com", "9999999999"},
new string[]{"chandu","chandu#gmail.com","8888888888"},
new string[]{"naveen","naveen#gmail.com", "7777777777"},
new string[]{"ramu","ramu#gmail.com", "6666666666"},
new string[]{"durga","durga#gmail.com", "5555555555"},
new string[]{"sagar","sagar#gmail.com", "4444444444"},
new string[]{"yadav","yadav#gmail.com", "3333333333"},
new string[]{"suraj","suraj#gmail.com", "2222222222"},
new string[]{"niharika","niharika#gmail.com","11111111111"},
new string[]{"anusha","anusha#gmail.com", "0000000000"},
};
for (int i =0; i < Members.Length; i++)
{
System.Console.Write("Name List ({0}):", i + 1);
for (int j = 0; j < Members[i].Length; j++)
{
System.Console.Write(Members[i][j] + "\t");
}
System.Console.WriteLine();
}``
Console.ReadKey();
}
}
}
The above is the code for my C# console program in which i used jagged array and i assigned values manually but now my requirement is 'without assigning manually into array i want the same details to import into my program from an csv file(which is at some location in my disc). So how to do it what functions should i make use , please help me with some example. Thank you.
static void Main()
{
string csv_file_path=#"C:\Users\Administrator\Desktop\test.csv";
DataTable csvData = GetDataTabletFromCSVFile(csv_file_path);
Console.WriteLine("Rows count:" + csvData.Rows.Count);
Console.ReadLine();
}
private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
{
DataTable csvData = new DataTable();
try
{
using(TextFieldParser csvReader = new TextFieldParser(csv_file_path))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
}
csvData.Rows.Add(fieldData);
}
}
}
catch (Exception ex)
{
}
return csvData;
}
Treat the CSV file like an excel workbook and you will find a lot of examples on the web for what you need to do.
ExcelFile ef = new ExcelFile();
// Loads file.
ef.LoadCsv("filename.csv");
// Selects first worksheet.
ExcelWorksheet ws = ef.Worksheets[0];
I won't go into details, but you can read lines text from a file with File.ReadAllLines.
Once you have those lines, you can split them into parts using String.Split (at least this will work if the CSV file contains very simple information as in your example).

Resources