您好我正在使用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=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");
答案 0 :(得分:1)
问题是您在构造insert命令时将整数转换为字符串文字。见下文:
OleDbCommand cmd = new OleDbCommand("INSERT INTO [Sheet1$]([Total],[Closed],[Issues],[Cancelled],[Back out]) VALUES('" + v1 + "','" + v2 + "','" + v3 + "','" + v4 + "','" + v5 + "')", conn);
将该行更改为:
OleDbCommand cmd = new OleDbCommand("INSERT INTO [Sheet1$]([Total],[Closed],[Issues],[Cancelled],[Back out]) VALUES(" + v1 + "," + v2 + "," + v3 + "," + v4 + "," + v5 + ")", conn);
这可以解决你所有的问题: - )
注意:手动构造SQL很容易出错。整数是你最不担心的。想象一下,尝试插入恰好包含单引号字符(')的字符串,当您将文化设置为使用逗号作为小数点分隔符,日期等等时,浮点数。就个人而言,我更喜欢使用EPPlus(XLSX)和ExcelLibrary(XLS)等库。两者都是LGPL许可,并且比您当前使用的方法具有更大的灵活性。它们不需要在机器上安装Excel,这使它们成为服务器端Excel报告生成的理想选择。此外,它们有助于避免使用Microsoft Office Interop的各种内存泄漏问题解决方案。
答案 1 :(得分:0)
此代码段帮助我处理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();