How to insert UTC datetimes to closedXML? - c#

In the c# closedXML plugin, I get this message
Unable to set cell value to 10/27/2017 10:14:23 AM +00:00
c#
public static async Task<string> SaveToExcel(string command, Dictionary<string, object> h, string domain, string sheet_name, string[] headers)
{
SqlConnection myConnection = new SqlConnection(GetConnectionString(domain));
await myConnection.OpenAsync();
SqlCommand myCommand = new SqlCommand(command, myConnection);
foreach (KeyValuePair<string, object> entry in h)
{
myCommand.Parameters.AddWithValue(entry.Key, entry.Value ?? DBNull.Value);
}
System.Diagnostics.Debug.WriteLine(myCommand.CommandText);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = myCommand;
DataTable dt = new DataTable();
sda.Fill(dt);
// add headers
DataRow dr = dt.NewRow();
for(int i=0; i<headers.Length; i++)
{
//dr[i] = headers[i];
}
//dt.Rows.InsertAt(dr, 0);
string filepath = Excel.SaveExcel(dt, sheet_name);
dt.Dispose();
sda.Dispose();
myCommand.Dispose();
myConnection.Close();
return filepath;
}
public static string SaveExcel(System.Data.DataTable dt, string SheetName)
{
// create the Excel workbook
var wb = new XLWorkbook();
// creates the worksheet
var ws = wb.AddWorksheet(SheetName);
// add data
ws.Cell(1, 1).InsertTable(dt);
// the range for which you want to add a table style
var range = ws.Range(1, 1, dt.Rows.Count, dt.Columns.Count);
// create the actual table
var table = range.CreateTable();
// adjust size
ws.Columns().AdjustToContents();
// apply style
table.Theme = XLTableTheme.TableStyleMedium5;
// save file as temp
string result = Path.GetTempFileName();
wb.SaveAs(result);
return result;
}
How can I fix this?
Thanks

Related

Read a single cell from Excel to a string using C# and ASP.NET

I need to read a single cell from an xsl excel file to a string withing the web application i am building. I was previously pulling cell ranges from the file to a table using the following code:
string PullFromExcell(string CellNo)
{
string cell;
string properties = String.Format(#"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Users\User\Desktop\file.xls; Extended Properties = 'Excel 8.0;'");
using (OleDbConnection conn = new OleDbConnection(properties))
{
string worksheet = "Sheet";
conn.Open();
DataSet ds = new DataSet();
using (OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [" + worksheet + "$" + CellNo + "]", properties))
{
DataTable dt = new DataTable();
cell = dt.ToString();
da.Fill(dt);
ds.Tables.Add(dt);
grdComponent.DataSource = dt;
grdComponent.DataBind();
}
}
return cell;
}
How would i send that to a string? The code that i would use when pulling from a database is similar to this:
Sqlstring = "Select data from variable where name = 'fred' and ssn = 1234";
var cmd0 = new SqlCommand(Sqlstring, Class_Connection.cnn);
string Data = cmd0.ExecuteScalar().ToString();
i'm just not sure if any of that is compatible.
After filling DataTable, you can search the row like this:
foreach (DataRow dr in dt.Rows)
{
if (dr["name"] == "fred" && dr["ssn"] == "1234")
{
cell = dr["data"].ToString();
break;
}
}

How to populate a listview from a datatable that is using stored procedures

I took over a project from a former coworker and I am trying to populate a listview from a datatable that pulls from a mysql database.
Here is what I am using to pull from MySQL:
internal static DataTable GetDataSPT3(string StoredProcedure, Dictionary<string, string> Parameters)
{
DataTable _DT = new DataTable();
MySqlCommand cmd = new MySqlCommand(StoredProcedure, new MySqlConnection(Creds.GetConnectionStringT3()));
cmd.CommandType = CommandType.StoredProcedure;
foreach (KeyValuePair<string, string> Parameter in Parameters)
cmd.Parameters.Add(new MySqlParameter(Parameter.Key, Parameter.Value));
cmd.Connection.Open();
MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
_DT.Load(dr);
dr.Close();
return _DT;
}
This is what I am using to populate the listview:
private void GetMetrics()
{
string begin = "2015-05-26 00:00:00";
string end = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Dictionary<string, string> Parameters = GetParameters();
Parameters.Add("p_daily_metrics_date_Date1", begin);
Parameters.Add("p_daily_metrics_date_Date2", end);
DataTable dt = new DataTable();
foreach (DataRow row in RRCStoredProcedures.GetDataSPT3("spGetDailyMetrics", Parameters).Rows)
{
ListViewItem item = new ListViewItem(row[0].ToString());
for (int i = 0; i < row.Table.Rows.Count;i++)
{
item.SubItems.Add(row[i].ToString());
}
lvMetrics.Items.Add(item);
}
}
Watching the process work with breakpoints and the SQL is finding data and returning data, but not in any useful way.
What am I doing wrong?
This is what I came up with, I'm still learning, to solve my issue. A DatagridView did not work for me since it fires a selectionchanged event when it loads and due to the needs of the form I could not have that.
My solution:
private void GetMetrics()
{
string[] array = new string[11];
string begin = "2015-05-26 00:00:00";
string end = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Dictionary<string, string> Parameters = GetParameters();
Parameters.Add("p_daily_metrics_date_Date1", begin);
Parameters.Add("p_daily_metrics_date_Date2", end);
foreach (DataRow row in (RRCStoredProcedures.GetDataSPT4("spGetDailyMetrics", Parameters)).Rows)
{
for (int item = 0; item < row.ItemArray.Count(); item++)
{
array[item] = row.ItemArray[item].ToString();
}
ListViewItem thing = new ListViewItem
(new[] { array[0], array[1], array[2], array[3], array[4], array[5], array[6], array[7], array[8], array[9], array[10] });
Metricslstvw.Items.Insert(0, thing);
Array.Clear(array, 0, 10);
}
foreach(ColumnHeader ch in Metricslstvw.Columns) { ch.Width = -2; }
}
Since the DataRow exposes ItemArray, I just rolled that into an array I know how to use.

Get DocumentText from Excel

I need to get all document formatted text from an excel spreadsheet.
I need a better solution that this. This achieves my goal but does not scale at all!
StringBuilder strData = new StringBuilder();
var worksheets = ReferenceDocument.Worksheets;
foreach (Excel._Worksheet worksheet in worksheets)
{
foreach (var cell in worksheet.UsedRange.Cast<Excel.Range>())
{
object value = cell.Text;
string strValue = value == null ? null : value.ToString();
if (!String.IsNullOrWhiteSpace(strValue)) strData.AppendLine(strValue);
}
}
EDIT: I've tried calling worksheet.UsedRange.Text to get an array of strings, but unfortunately it returns System.DbNull and not an array.
I couldn't find anything in the Excel object model I could use. I solved issue by using OleDb
StringBuilder strData = new StringBuilder();
ReferenceDocument.SaveCopyAs(strSomeTempFile);
var cnnStr = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended properties=\"Excel 8.0;IMEX=1;HDR=NO\"", strSomeTempFile);
var cnn = new OleDbConnection(cnnStr);
try
{
cnn.Open();
var schemaTable = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
foreach(DataRow schemaRow in schemaTable.Rows)
{
string worksheetName = schemaRow["table_name"].ToString().Replace("'", "");
string sql = String.Format("select * from [{0}]", worksheetName);
var da = new OleDbDataAdapter(sql, cnn);
var dt = new DataTable();
da.Fill(dt);
foreach (DataRow row in dt.Rows)
foreach (DataColumn col in dt.Columns)
{
var value = row[col.ColumnName];
if (!(value is System.DBNull)) strData.AppendLine(value.ToString());
}
}
}
catch (Exception e)
{
// handle error
throw;
}
finally
{
cnn.Close();
}
return strData;

Display Json Values using webapi

I have datatable for fetching my datas from mysql table and i using a foreach loop for getting each value.So i want to convert each value to json and need to display all these values.How can i possible?
My Table
public static DataTable GetAlldata()
{
try
{
string connString = "Server=localhost;database=mytable;uid=myid;";
string query = "SELECT Tname FROM `mytable`.`tdetails`";
MySqlDataAdapter ma = new MySqlDataAdapter(query, connString);
DataSet DS = new DataSet();
ma.Fill(DS);
return DS.Tables[0];
}
catch (MySqlException e)
{
throw new Exception(e.Message);
}
}
public string jsonvalues()
{
string s = "";
RootObject ro = new RootObject();
DataTable dtaltheat = GetAlldata();
foreach (DataRow drow in dtaltheat.Rows)
{
string theatnme = drow["TheatreName"].ToString();
JavaScriptSerializer ser = new JavaScriptSerializer();
s = ser.Serialize(theatnme);
List<string> List = new List<string>();
List.Add(s);
}
return s;
}
This is the present status of my code.But it doesn't work..I am new in this field,so help me..

The First Column of the excel file to put in string variable C#?

What i need id to take the first column of an excel file and put that column in a string variable but numbers that will be taken from excel to be separated with blank space.
For Example:
Excel file:
1
3
4
56
76
7
876
23
43
(in column)
and string in C#
string number = "1 3 4 56 76 7 876 23 43"
Any code will help me
I have made the connection with excel file and i have created the dataset/datatable
but now i can not take those numbers as the sample above ?
If you have the data in a datset...you can do something like this....
////TRY THIS!!!
DataSet dsExcelContent = new DataSet();
//Fill from db
//
StringBuilder builder = new StringBuilder();
foreach (DataRow row in dsExcelContent.Tables[0].Rows)
{
builder.Append(row[0].ToString());
builder.Append(" ");
}
Console.WriteLine(builder.ToString());
.....More exact to your code....
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [sheet1$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet dsExcelContent = new DataSet();
DataTable dsExcelContent1 = new DataTable();
objAdapter1.Fill(dsExcelContent);
dataGridView1.DataSource = dsExcelContent1;
objConn.Close();
int test = dsExcelContent.Tables[0].Rows.Count;
StringBuilder builder = new StringBuilder();
foreach (DataRow row in dsExcelContent.Tables[0].Rows)
{
builder.Append(row[0].ToString());
builder.Append(" ");
}
//s has the data you want.....
string s = builder.ToString();
//REST OF YOUR CODE.....
Have a link - Reading Excel Document
Here's what I would do...first open the spreadsheet with this code.
xlApp = New Excel.Application
filePath = "FILENAME"
xlWorkBook = xlApp.Workbooks.Open(filePath)
xlWorkSheet = xlWorkBook.Worksheets(1)
Then read through the column like this
For data As Integer = 0 To 8
Dim obj As Excel.Range = CType(xlWorkSheet.Cells(data, 1), Range)
If obj.Value IsNot Nothing Then
MyArray(data) = obj.Value
Else
Exit Do
End If
Next
Then you should have all your data in MyArray then loop through and create a string with spaces and you should be good. This code is in VB but it should give you a good idea of the commands and structure to use.
private void Form1_Load(object sender, EventArgs e)
{
String sConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=unsorted.xls;Extended Properties=""Excel 12.0;HDR=NO;""";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [sheet1$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet dsExcelContent = new DataSet();
DataTable dsExcelContent1 = new DataTable();
objAdapter1.Fill(dsExcelContent);
dataGridView1.DataSource = dsExcelContent1;
objConn.Close();
int test = dsExcelContent.Tables[0].Rows.Count;
foreach(DataRow row in dsExcelContent.Tables[0].Rows)
{
StringBuilder builder = new StringBuilder();
foreach (DataColumn col in dsExcelContent.Tables[0].Columns)
{
builder.Append(row[col].ToString());
builder.Append(" ");
}
string s = builder.ToString();
this.label1.Text = s;
string[] numbers = s.Split(' ');
ArrayList numberList = new ArrayList();
int i;
foreach (String num in numbers)
{
if (Int32.TryParse(num, out i))
{
numberList.Add(i);
}
else
Console.WriteLine("'{0}' is not a number!", num);
}
this.listBox1.DataSource = numberList;
}
}
}
You just want to loop through the rows in the Dataset now:
var numberStr = EmptyStr;
foreach (DataRow dr in MyDataSet.Tables[0].Rows)
{
numberStr = EmptyStr ? numberStr += dr[0].ToString() : numberStr += " " + dr[0].ToString();
}
Updated Solution
private void Form1_Load(object sender, EventArgs e)
{
String s = String.Empty;
String sConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;DataSource=unsorted.xls;Extended Properties=""Excel 12.0;HDR=NO;""";
using (OleDbConnection conn = new OleDbConnection(sConnectionString))
{
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow schemaRow in schemaTable.Rows)
{
string sheet = schemaRow["TABLE_NAME"].ToString();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn);
cmd.CommandType = CommandType.Text;
DataTable outputTable = new DataTable(sheet);
output.Tables.Add(outputTable);
new OleDbDataAdapter(cmd).Fill(outputTable);
}
// populate string with value from rows
foreach (DataRow dr in MyDataSet.Tables[0].Rows)
{
s = String.Empty ? s += dr[0].ToString() : s += " " + dr[0].ToString();
}
dataGridView1.DataSource = dsExcelContent1;
objConn.Close();
}
this.label1.Text = s;
string[] numbers = s.Split(' ');
ArrayList numberList = new ArrayList();
int i;
foreach (String num in numbers)
{
if (Int32.TryParse(num, out i))
{
numberList.Add(i);
}
else
{
Console.WriteLine("'{0}' is not a number!", num);
}
}
this.listBox1.DataSource = numberList;
}

Categories

Resources