将数据从c#文本框插入excel
Hi im取消Oledb以从多个文本框向excel工作表插入数据。我的问题是,它不是作为数字类型进入excel,而是作为字符串,因此grpah不能在日期之外自动执行。 如何将值作为数字类型插入excel?我尝试将文本框转换为int,但没有成功 我的代码如下将数据从c#文本框插入excel,c#,visual-studio-2010,excel,C#,Visual Studio 2010,Excel,Hi im取消Oledb以从多个文本框向excel工作表插入数据。我的问题是,它不是作为数字类型进入excel,而是作为字符串,因此grpah不能在日期之外自动执行。 如何将值作为数字类型插入excel?我尝试将文本框转换为int,但没有成功 我的代码如下 string szConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=L://Metrics_gen.xlsx;Extended Properties='Excel 8.0;HDR=
string szConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=L://Metrics_gen.xlsx;Extended Properties='Excel 8.0;HDR=YES;'";
OleDbConnection conn = new OleDbConnection(szConn);
int v1 = Convert.ToInt32(textBox1.Text);
int v2 = Convert.ToInt32(textBox2.Text);
int v3 = Convert.ToInt32(textBox3.Text);
int v4 = Convert.ToInt32(textBox4.Text);
int v5 = Convert.ToInt32(textBox5.Text);
conn.Open();
OleDbCommand cmd = new OleDbCommand("INSERT INTO [Sheet1$]([Total],[Closed],[Issues],[Cancelled],[Back out]) VALUES('" + v1 + "','" + v2 + "','" + v3 + "','" + v4 + "','" + v5 + "')", conn);
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("complete");
这个代码片段帮助我在C#中处理Excel电子表格,特别是输入数字行,然后生成数据总和。如果在将数据加载到excel后处理代码中的行,请确保忽略标题行,这应该是显而易见的,但在这里仅提及它,以防出现问题 以下代码需要声明-使用Excel=Microsoft.Office.Interop.Excel
Excel.Application oXL;
Excel.Workbook oWB;
Excel.Worksheet oSheet;
Excel.Range oRange;
var filepath = "/somefilepath";
// Start Excel and get Application object.
oXL = new Excel.Application();
// Set some properties
oXL.Visible = true;
oXL.DisplayAlerts = false;
// Get a new workbook.
oWB = oXL.Workbooks.Add(Missing.Value);
// Get the Active sheet
oSheet = (Excel.Worksheet)oWB.ActiveSheet;
oSheet.Name = "Data";
int rowCount = 1;
string strExpr;
string strSort;
foreach (DataRow dr in dt.Rows)
{
rowCount += 1;
for (int i = 1; i < dt.Columns.Count + 1; i++)
{
// Add the header the first time through
if (rowCount == 2)
{
oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
}
oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
}
}
//Freze the first row
oSheet.Application.ActiveWindow.SplitRow = 1;
oSheet.Application.ActiveWindow.FreezePanes = true;
// Resize the columns
oRange = oSheet.get_Range(oSheet.Cells[1, 1],
oSheet.Cells[rowCount, dt.Columns.Count]);
oRange.EntireColumn.AutoFit();
// Save the sheet and close
oSheet = null;
oRange = null;
oWB.SaveAs(filepath, Excel.XlFileFormat.xlWorkbookNormal,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlExclusive,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
oWB.Close(Missing.Value, Missing.Value, Missing.Value);
oWB = null;
oXL.Quit();
Excel.applicationoxl;
Excel.oWB工作手册;
Excel.oSheet工作表;
Excel.Range橙色;
var filepath=“/somefilepath”;
//启动Excel并获取应用程序对象。
oXL=新的Excel.Application();
//设置一些属性
可见=真;
oXL.DisplayAlerts=false;
//获取新工作簿。
oWB=oXL.Workbooks.Add(缺少.Value);
//获取活动工作表
oSheet=(Excel.Worksheet)oWB.ActiveSheet;
oSheet.Name=“数据”;
int rowCount=1;
字符串strExpr;
字符串strSort;
foreach(数据行dr在dt.行中)
{
行计数+=1;
对于(int i=1;i
这个代码片段帮助我用C#处理Excel电子表格,特别是输入数字行,然后生成数据总和。如果在将数据加载到excel后处理代码中的行,请确保忽略标题行,这应该是显而易见的,但在这里仅提及它,以防出现问题
以下代码需要声明-使用Excel=Microsoft.Office.Interop.Excel
Excel.Application oXL;
Excel.Workbook oWB;
Excel.Worksheet oSheet;
Excel.Range oRange;
var filepath = "/somefilepath";
// Start Excel and get Application object.
oXL = new Excel.Application();
// Set some properties
oXL.Visible = true;
oXL.DisplayAlerts = false;
// Get a new workbook.
oWB = oXL.Workbooks.Add(Missing.Value);
// Get the Active sheet
oSheet = (Excel.Worksheet)oWB.ActiveSheet;
oSheet.Name = "Data";
int rowCount = 1;
string strExpr;
string strSort;
foreach (DataRow dr in dt.Rows)
{
rowCount += 1;
for (int i = 1; i < dt.Columns.Count + 1; i++)
{
// Add the header the first time through
if (rowCount == 2)
{
oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
}
oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
}
}
//Freze the first row
oSheet.Application.ActiveWindow.SplitRow = 1;
oSheet.Application.ActiveWindow.FreezePanes = true;
// Resize the columns
oRange = oSheet.get_Range(oSheet.Cells[1, 1],
oSheet.Cells[rowCount, dt.Columns.Count]);
oRange.EntireColumn.AutoFit();
// Save the sheet and close
oSheet = null;
oRange = null;
oWB.SaveAs(filepath, Excel.XlFileFormat.xlWorkbookNormal,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlExclusive,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
oWB.Close(Missing.Value, Missing.Value, Missing.Value);
oWB = null;
oXL.Quit();
Excel.applicationoxl;
Excel.oWB工作手册;
Excel.oSheet工作表;
Excel.Range橙色;
var filepath=“/somefilepath”;
//启动Excel并获取应用程序对象。
oXL=新的Excel.Application();
//设置一些属性
可见=真;
oXL.DisplayAlerts=false;
//获取新工作簿。
oWB=oXL.Workbooks.Add(缺少.Value);
//获取活动工作表
oSheet=(Excel.Worksheet)oWB.ActiveSheet;
oSheet.Name=“数据”;
int rowCount=1;
字符串strExpr;
字符串strSort;
foreach(数据行dr在dt.行中)
{
行计数+=1;
对于(int i=1;i
问题在于,在构造insert命令时,您正在将整数转换为字符串文字。见下文:
OleDbCommand cmd=new-OleDbCommand(“插入到[Sheet1$]([Total]、[Closed]、[Issues]、[Cancelled]、[Back out])值(“+v1+”、“+v2+”、“+v3+”、“+v4+”、“+v5+”),康涅狄格州);
将该行更改为:
OleDbCommand cmd=新的OleDbCommand(“插入到[Sheet1$]([Total]、[Closed]、[Issues]、[Cancelled]、[Back out])值(“+v1+”、“+v2+”、“+v3+”、“+v4+”、“+v5+”),康涅狄格州);
这个应该