Reading columns from Excel, reformat cells - c#

I am currently trying to read in cells from an excel spread sheet, and it seems to reformat cells when I don't want it to. I want it to come through as plan text. I have read a couple of solutions to this problem and I have implemented them, but I am still having the same issue.
The reader turns dates in numbers and numbers into dates.
Example:
Friday, January 29, 2016 comes out to be : 42398
and
40.00 comes out to be : 2/9/1900 12:00:00 AM
code:
string stringconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + files[0] + ";Extended Properties=\"Excel 12.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\"";
try {
OleDbConnection conn = new OleDbConnection(stringconn);
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [CUAnswers$]", conn);
DataTable dt = new DataTable();
try {
printdt(dt);
I have tried
IMEX=0;
HDR=NO;
TypeGuessRows=1;
This is how I am printing out the sheet
public void printdt(DataTable dt) {
int counter1 = 0;
int counter2 = 0;
string temp = "";
foreach (DataRow dataRow in dt.Rows) {
foreach (var item in dataRow.ItemArray) {
temp += " ["+counter1+"]["+counter2+"]"+ item +", ";
counter2++;
}
counter1++;
logger.Debug(temp);
temp = "";
counter2 = 0;
}
}

I had a similar problem, except it was using Interop to read the Excel spreadsheet. This worked for me:
var value = (range.Cells[rowCnt, columnCnt] as Range).Value2;
string str = value as string;
DateTime dt;
if (DateTime.TryParse((value ?? "").ToString(), out dt))
{
// Use the cell value as a datetime
}
Editted to add new ideas
I was going to suggest saving the spreadsheet as comma-separated values. Then Excel converts the cells to text. It is easy to parse a CSV in C#.
That led me to think of how to programmatically do the conversion, which is covered in Convert xls to csv programmatically. Maybe the code in the accepted answer is what you are looking for:
string ExcelFilename = "c:\\ExcelFile.xls";
DataTable worksheets;
string connectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;" + #"Data Source=" + ExcelFilename + ";" + #"Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
worksheets = connection.GetSchema("Tables");
foreach (DataRow row in worksheets.Rows)
{
// For Sheets: 0=Table_Catalog,1=Table_Schema,2=Table_Name,3=Table_Type
// For Columns: 0=Table_Name, 1=Column_Name, 2=Ordinal_Position
string SheetName = (string)row[2];
OleDbCommand command = new OleDbCommand(#"SELECT * FROM [" + SheetName + "]", connection);
OleDbDataAdapter oleAdapter = new OleDbDataAdapter();
oleAdapter.SelectCommand = command;
DataTable dt = new DataTable();
oleAdapter.FillSchema(dt, SchemaType.Source);
oleAdapter.Fill(dt);
for (int r = 0; r < dt.Rows.Count; r++)
{
string type1 = dr[1].GetType().ToString();
string type2 = dr[2].GetType().ToString();
string type3 = dr[3].GetType().ToString();
string type4 = dr[4].GetType().ToString();
string type5 = dr[5].GetType().ToString();
string type6 = dr[6].GetType().ToString();
string type7 = dr[7].GetType().ToString();
}
}
}

Related

How do you programmatically check if a spreadsheet has headers in C#

I am creating a winform application where every day, a user will select a xlsx file with the day's shipping information to be merged with our invoicing data.
The challenge I am having is when the user does not download the xlsx file with the specification that the winform data requires. (I wish I could eliminate this step with an API connection but sadly I cannot)
My first step is checking to see if the xlsx file has headers to that my file path is valid
Example
string connString = "provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + *path* + "';Extended Properties='Excel 12.0;HDR=YES;';";
Where path is returned from an OpenFileDialog box
If the file was chosen wasn't downloaded with headers the statement above throws an exception.
If change HDR=YES; to HDR=NO; then I have trouble identifying the columns I need and if the User bothered to include the correct ones.
My code then tries to load the data into a DataTable
private void loadRows()
{
for (int i = 0; i < deliveryTable.Rows.Count; i++)
{
DataRow dr = deliveryTable.Rows[i];
int deliveryId = 0;
bool result = int.TryParse(dr[0].ToString(), out deliveryId);
if (deliveryId > 1 && !Deliveries.ContainsKey(deliveryId))
{
var delivery = new Delivery(deliveryId)
{
SalesOrg = Convert.ToInt32(dr[8]),
SoldTo = Convert.ToInt32(dr[9]),
SoldName = dr[10].ToString(),
ShipTo = Convert.ToInt32(dr[11]),
ShipName = dr[12].ToString(),
};
Which all works only if the columns are in the right place.
If they are not in the right place my thought is to display a message to the user to get the right information
Does anyone have any suggestions?
(Sorry, first time posting a question and still learning to think through it)
I guess you're loading the spreadsheet into a Datatable? Hard to tell with one line of code. I would use the columns collection in the datatable and check to see if all the columns you want are there. Sample code to enumerate the columns below.
private void PrintValues(DataTable table)
{
foreach(DataRow row in table.Rows)
{
foreach(DataColumn column in table.Columns)
{
Console.WriteLine(row[column]);
}
}
}
private void GetExcelSheetForUpload(string PathName, string UploadExcelName)
{
string excelFile = "DateExcel/" + PathName;
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
try
{
DataSet dss = new DataSet();
String connString = "Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=True;Extended Properties=Excel 12.0 Xml;Data Source=" + PathName;
objConn = new OleDbConnection(connString);
objConn.Open();
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return;
}
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow row in dt.Rows)
{
if (i == 0)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + excelSheets[i] + "]", objConn);
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
oleda.Fill(dss, "TABLE");
}
i++;
}
grdExcel.DataSource = dss.Tables[0].DefaultView;
grdExcel.DataBind();
lblTotalRec.InnerText = Convert.ToString(grdExcel.Rows.Count);
}
catch (Exception ex)
{
ViewState["Fuletypeidlist"] = "0";
grdExcel.DataSource = null;
grdExcel.DataBind();
}
finally
{
if (objConn != null)
{
objConn.Close();
objConn.Dispose();
}
if (dt != null)
{
dt.Dispose();
}
}
}
if (grdExcel.HeaderRow.Cells[0].Text.ToString() == "CODE")
{
GetExcelSheetForEmpl(PathName);
}
else
{
divStatusMsg.Style.Add("display", "");
divStatusMsg.Attributes.Add("class", "alert alert-danger alert-dismissable");
divStatusMsg.InnerText = "ERROR !!... Upload Excel Sheet in header Defined Format ";
}

Can't export large data from oracle to excel file using c#

I have a problem with extracting large data from oracle table to C#, and I
couldn't find the solution myself.
For this task I wrote a C# code, which loaded data from oracle procedure, which returns cursor, in excel file for the first time.
But when I tried to load bigger table (about 20 columns and 90 000 rows), it just didn't work.
Script doesn't fall with error, but data are not inserted into excel file.
I tried to load for 10 000 rows and then save the results, but again, only 30 000 rows were inserted.
I monitored the counter in loop, it is going correct and reach needed 90 000 and ExecuteNonQuery() always returned the value 10 000. But when I open excel file, there are only 30 000 rows there.
Can you please help me to catch the error, or may be somebody met the same problem, and can advise me what to do or what to read.
Thank you for any help!
I didn't write the connection string, but I think, it's correct, cause script works correctly with small datatable.
public static void Main()
{
string datetime = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
try
{
OleDbConnection Excel_OLE_Con = new OleDbConnection();
OleDbCommand Excel_OLE_Cmd = new OleDbCommand();
string qwe_constr = "connection string";
OracleConnection myADONETConnection = new OracleConnection(qwe_constr);
string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + "E:\\qaz\\15.07.2016\\qwe" +
";" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;\"";
File.Delete("E:\\qaz\\15.07.2016\\qwe.xlsx");
//fill datatable with data for insert
myADONETConnection.Open();
OracleCommand cmd_proc = new OracleCommand();
cmd_proc.Connection = myADONETConnection;
cmd_proc.CommandType = System.Data.CommandType.StoredProcedure;
cmd_proc.CommandText = "procedure_name";
cmd_proc.Parameters.Add("p_show_del", OracleDbType.Int16).Value = 0;
cmd_proc.Parameters.Add("p_type", OracleDbType.Varchar2, 3).Value = "INV";
cmd_proc.Parameters.Add("p_errno", OracleDbType.Int16).Value = 157;
cmd_proc.Parameters.Add("outcur", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
DataTable dt_with_data = new DataTable();
dt_with_data.Load(cmd_proc.ExecuteReader());
myADONETConnection.Close();
//string with column headers
string TableColumns = "";
foreach (DataColumn column in dt_with_data.Columns)
{
TableColumns += column + "],[";
}
// Replace most right comma from Columnlist
TableColumns = ("[" + TableColumns.Replace(",", " Text,").TrimEnd(','));
TableColumns = TableColumns.Remove(TableColumns.Length - 2);
//Use OLE DB Connection and Create Excel Sheet
Excel_OLE_Con.ConnectionString = connstring;
Excel_OLE_Con.Open();
Excel_OLE_Cmd.Connection = Excel_OLE_Con;
Excel_OLE_Cmd.CommandText = "Create table [sheet1] (" + TableColumns + ")";
Excel_OLE_Cmd.ExecuteNonQuery();
Excel_OLE_Con.Close();
//Write Data to Excel Sheet from DataTable dynamically
//string with command
Excel_OLE_Con.Open();
String sqlCommandInsert = "";
String sqlCommandValue = "";
foreach (DataColumn dataColumn in dt_with_data.Columns)
{
sqlCommandValue += dataColumn + "],[";
}
sqlCommandValue = "[" + sqlCommandValue.TrimEnd(',');
sqlCommandValue = sqlCommandValue.Remove(sqlCommandValue.Length - 2);
sqlCommandInsert = "INSERT into [sheet1] (" + sqlCommandValue + ") VALUES(";
int columnCount = dt_with_data.Columns.Count;
int i_qaz = 0;
foreach (DataRow row in dt_with_data.Rows)
{
i_qaz++;
Console.WriteLine(i_qaz.ToString());
string columnvalues = "";
for (int i = 0; i < columnCount; i++)
{
int index = dt_with_data.Rows.IndexOf(row);
columnvalues += "'" + dt_with_data.Rows[index].ItemArray[i].ToString().Replace("'", "''") + "',";
}
columnvalues = columnvalues.TrimEnd(',');
var command = sqlCommandInsert + columnvalues + ")";
Excel_OLE_Cmd.CommandText = command;
Excel_OLE_Cmd.ExecuteNonQuery();
}
}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText("E:\\qaz\\15.07.2016\\qwe_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());
}
}
}
PS: Same question in Russian.

Calculate Excel value before import to SQL Server

I have problem about import to SQL Server, the scenario is to import excel file and calculate the value in column 3 and 4 (produce column 5) of the imported excel file. In my case, the calculation is in C#, not in excel. And then import to SQL Server (ASP.Net + C#). Any idea how to do this ?
This is my code (it's still give me error)
protected void btnImport_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
if (Extension == ".xlsx")
{
string path = string.Concat((Server.MapPath("~/tampung/" + FileUpload1.FileName)));
FileUpload1.PostedFile.SaveAs(path);
//make connection to excel workBook
using (OleDbConnection oledbcon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;"))
{
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbcon);
OleDbDataAdapter ObjAdapter1 = new OleDbDataAdapter(cmd);
oledbcon.Open();
using (DbDataReader dr = cmd.ExecuteReader())
{
DataTable DT = new DataTable();
DT.Load(dr);
for (int i = 0; i < DT.Rows.Count; i++)
{
string Year = DT.Rows[i][0].ToString();
string StudentName = DT.Rows[i][1].ToString();
string Semester = DT.Rows[i][2].ToString();
decimal Value1 = Convert.ToDecimal(DT.Rows[i][3]);
decimal Value2 = Convert.ToDecimal(DT.Rows[i][4]);
decimal AverageValue = Convert.ToDecimal((Value1 + Value2) / 2);
}
string conString = #"Data Source=PETRELLI;Initial Catalog=demo;Integrated Security=True";
SqlBulkCopy bulkInsert = new SqlBulkCopy(conString);
bulkInsert.DestinationTableName = "student";
bulkInsert.WriteToServer(dr);
oledbcon.Close();
Array.ForEach(Directory.GetFiles(Server.MapPath("~/temp/")), File.Delete);
Label1.Text = "Succeeded";
}
}
}
else
{
Label1.Text = "Hi, it's error";
}
}
else
{
Label1.Text = "Please choose the right file excel";
}
}
The error should be coming from this line
bulkInsert.WriteToServer(dr);
This is happening because DT.Load(dr), that you called earlier, have looped through the dr already and has moved the pointer to the end, after which you can not use the dr again.
DT.Load(dr); //<-- this line already looped through the dr. dr can't be used after this line
Solution
Use the DT instead of dr since you already have DT populated with the required data by calling DT.Load(dr)

In Excel how to search a value in a column and get all the values in that row using C#

I am trying searching a value on column "C" and getting a matched cell name as well, for example C14, now how can I select the values in row 14.
I tried as :
private static MyObject GetRowValue(int rowNumber)
{
string connString = "";
string path = "C:\\Code\\MyFile.xls";
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
string query = "SELECT * FROM [Sheet1$A" + rowNumber + ":BD" + rowNumber + "]";
using (OleDbConnection connection = new OleDbConnection(connString))
{
var adapter = new OleDbDataAdapter(query, connection);
DataSet ds = new DataSet();
adapter.Fill(ds);
DataTable dt = ds.Tables[0];
}
}
If row number is 10, them I am trying to get all values of 10th row only, but it is returning all the rows after 10th row.
Just use this formula:
string query = #"SELECT * FROM [Sheet1$"+ (rowNumber-1) + ":" + (rowNumber) + "]";
If rowNumber=10 then you get all the values from the 10th row.
Was this helpful?
If it were me, I'd let Excel do the work for me. You'd need the Office.Interop.Excel namespace.
private static ReadRows(string SearchValue, int StartRow)
{
int r = StartRow;
Excel.Application xl = new Excel.Application();
xl.Workbooks.Open(your workbook);
Excel.WorkSheet ws = xl.Workbooks(1).Worksheets(1);
do
{
if(ws.Cells(r,3).value == SearchValue)
{
// read the entire row
string colA = ws.Cells(r,1).value;
string colB = ws.Cells(r,2).value;
//...
// or loop through all columns
int c = 1;
do
{
// add cell value to some collection
c++;
} while (ws.Cells(r,c).Value != "");
}
r++;
} while (ws.Cells(r,3).Value != ""); // 3 because you want column C
}

Dot is implicitly converted into hash while converting data from Excel file to XML

I have succeeded in creating XML file from an Excel file using the following C# code:
protected void Button5_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
OleDbConnection ole = new OleDbConnection();
string s = Server.MapPath("../admin/ProductOptions");
s = s + "\\" + FileUpload1.FileName;
System.IO.File.Delete(s);
FileUpload1.PostedFile.SaveAs(s);
string path = s;
ole.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + path + ";" + "Extended Properties=" + "\"" + "Excel 12.0;HDR=YES;" + "\"";
OleDbCommand command = new OleDbCommand("select * from[SHEET1$]", ole);
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
GridView1.Visible = true;
string filepath = Server.MapPath("ProductOptions") + "\\" + DDLproduct.SelectedValue + ".xml";
Session["ss"] = ds;
write_to_xml(ds,filepath);
}
else
{
Label2.Visible = true;
Label2.Text="[Please Select a file]";
}
}
But the problem is when this code is converting the Excel Data to XML data, then dots are itself converted into Hash(Only First Row). I know the reason but don't know the solution.
It`s happening because of dots in Excel file when converted into XML tags them implicitly converted to HASH.......
Kindly suggest me, how can I stop this conversion?
Finally got the solution:
When OLEDB Adapter fills the data in DataSet, it converts DOT into HASH.
Now I have stored that data into a DataTable(dt) and then accessed the column name and replace HASH with DOT (using Replace method of String) and create a new DataTable(dt2) with new column names.
After this using two for loops, I have inserted data from first DataTable(dt) to new Datatable(dt2).
(*one loop for rows and another one for columns)
Finally bind the grid with new DataTable(dt2)
Following is the full code for that function:
if (FileUpload1.HasFile)
{
OleDbConnection ole = new OleDbConnection();
string s = Server.MapPath("../admin/ProductOptions");
s = s + "\\" + FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(s);
string path = s;
ole.ConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + path + ";" + "Extended Properties=" + "\"" + "Excel 12.0;HDR=YES;IMEX=2;READONLY=FALSE;" + "\" ";
OleDbCommand command = new OleDbCommand("select * from[SHEET1$]", ole);
DataSet ds = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter.Fill(ds);
DataTable dt = (DataTable)ds.Tables[0];
DataTable dt2 = new DataTable("dt2");
Session["dt"] = null;
for (int i = 0; i < dt.Columns.Count; i++)
{
string s2 = dt.Columns[i].ToString();
s2 = s2.Replace("#", ".");
string ProductName = s2.ToString();
if (Session["dt"] == null)
{
DataColumn dCol1 = new DataColumn(ProductName, typeof(System.String));
dt2.Columns.Add(dCol1);
}
}
for (int i = 0; i < dt.Rows.Count; i++)
{
dt2.Rows.Add();
for (int x = 0; x < dt.Columns.Count; x++)
{
dt2.Rows[i][x] = dt.Rows[i][x];
}
}
System.IO.File.Delete(s);
GridView1.DataSource = dt2;
GridView1.DataBind();
GridView1.Visible = true;
string filepath = Server.MapPath("ProductOptions") + "\\" + DDLproduct.SelectedValue + ".xml";
// Session["ss"] = ds;
write_to_xml(dt2,filepath);
}
else
{
Label2.Visible = true;
Label2.Text="[Please Select a file]";
}
Following is the code for write_to_xml() :
public void write_to_xml(DataTable dt, string path)
{
dt.WriteXml(path);
}
Any query or alternative solution would be appreciated... :)
Turn your headers off by HDR=No in your connection string and get your job done.
Before feeding them back to excel with HDR=Yes replace .s with #s using regex or any tool you want in the first row.
Instead of your solution I use Encoding.UTF8 in this way:
using (var fs = new FileStream(xmlFile, FileMode.CreateNew))
{
using (var xw = new XmlTextWriter(fs, Encoding.UTF8))
{
ds.WriteXml(xw);
}
}
And had no problem, this also converts < to < and > to >.

Categories