Tag: winforms

在Excel中使用不同types的信息在每一行中读取一列

我想用我编写的程序读取Excel文档,并根据读取的信息,更改我的用户界面上的某些字段,我遇到了一个有趣的问题。 我的一个专栏有可能保存4种不同types的数据。 行中的每个单元格都可能包含一个数字,一个以文本或文本forms存储的数字。 这是我用来阅读本专栏的部分代码: try { string dtTerritory = dt.Rows[0].Field<string>("Territory"); cmbTerritory.SelectedItem = dtTerritory; territory = dtTerritory; } catch { double dtTerritory = dt.Rows[0].Field<double>("Territory"); cmbTerritory.SelectedItem = dtTerritory.ToString(); territory = dtTerritory.ToString(); } 我遇到的问题是读取以文本forms存储的数字,问题是,如果数字以Excel文档forms存储在Excel工作表中,则始终以3个字符存储。 所以如果数字是3,那么如果它是46,则它被存储为“3”(3之后2个空格),它被存储为“46”(46之后1个空格)。 所以我基本上是寻找最好的解决scheme,我可以让我的程序忽略号码后发生的空间。 有任何想法吗?

我怎样才能让用户select一个Excel文件,并在WinForms应用程序中查询选定的文件?

我知道如何查询电子表格的某些结果; 那不是问题。 我无法find正确的方法来允许用户select应该被查询的文件。 在这一点上,我已经放弃了我的代码为这个努力。 如果有人能指引我正确的方向,我将不胜感激。

如何使用C#运行VBmacros脚本?

我有一个VB脚本macros用于Excel文件及其作品(手动)。 我正试图将其转换为小型APP使用: private static void RunMacro() { Excel.Application book = null; Excel.Workbooks workbooks = null; Excel.Workbook macroWorkbook = null; Excel.Workbook destinationWorkbook = null; try { book = new Excel.Application(); workbooks = book.Workbooks; macroWorkbook = workbooks.Open(System.Windows.Forms.Application.StartupPath + "\\NewMacro.xltm"); destinationWorkbook = workbooks.Open(System.Windows.Forms.Application.StartupPath + "\\TEST.csv"); book.Run("NewMacro.xltm"); macroWorkbook.Close(false); destinationWorkbook.Close(true); } catch (Exception ex) { throw ex; // the finally […]

将多维string数组导出到新的Excel电子表格

使用互操作我设法导入一个Excel文档(包含人口)到这种格式的多维string数组: public string[,] DataArray; 我用这个方法来填充数组: try { Excel.Application xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open(FilePath); Excel._Worksheet xlWorksheet = xlWorkBook.Sheets[1]; Excel.Range xlRange = xlWorksheet.UsedRange; rowCount = xlRange.Rows.Count; colCount = xlRange.Columns.Count; InitialiserTableauPopulation(rowCount, colCount); for (int x = 1; x <= colCount; x++) { for (int y = 1; y <= rowCount; y++) { DataArray[x – 1, y […]

将DataGridView数据保存到SQL Server数据库Winform

以下是UI的示例图像: 我正在处理一个项目,将导入一个Excel文件,并通过Windows窗体显示数据到DataGridView。 导入Excel文件并在DataGridView中显示工作正常,我遇到的问题是将数据保存在DataGridView中作为批量插入,当我点击保存button时,它显示 错误的屏幕截图: 在System.Data.dll中发生未处理的types为“System.ArgumentException”的exception 当我查看细节时显示: 不存在从对象typesSystem.Windows.Forms.DataGridViewTextBoxColumn到已知托pipe提供程序本机types的映射。 代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb; using System.Data.SqlClient; 保存button代码 private void btn_Save_Click(object sender, EventArgs e) { foreach (DataGridViewRow row in dataGridView1.Rows) { string constring = @"Data Source=databasename;Initial Catalog=Rookies;Integrated Security=True"; using(SqlConnection con = new […]

用对话框保存生成的excel文件

我从一个button点击数据表生成一个xls文件。 现在保存文件的path在函数中被硬编码以生成文件: Function CreateExcelFile(xlFile As String) As Boolean Try Dim xlRow As Integer = 2 Dim xlApp As New Microsoft.Office.Interop.Excel.Application Dim xlWB = xlApp.Workbooks.Add Dim xlWS = xlApp.Worksheets.Add Dim intStr As Integer = 0 Dim NewFile As String = "" Dim strCaption As String = "PSLF Driver Files Records" xlFile = Replace(xlFile, "Return Files", "Reports") […]

C#WinForm Excel检查空表function,但太慢(15张30秒)

我有一个工作函数,检查如果表格从Excel文件是空的。 每个工作表都会调用这个函数。 该function完美地工作。 唯一的问题是,如果我的Excel文件有50张,调用这个函数需要30秒,这太多了。 我可以做些什么来优化这个function,所以在excel中所有表单调用一次? 我应该使用LINQ还是OLEDB。 PS我不会使用Interlop,因为它将三倍的时间检查。 这是我的function public static bool isEmptySheet2(string nameSheet) { string filePath = Properties.Resources.ResourceManager.GetString("FilePath"); string fileName = Properties.Resources.ResourceManager.GetString("FileName"); string fileLocation = filePath + fileName; try { DataTable dt = new DataTable(); string connstr = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileLocation + ";" + "Extended Properties=" + "\"" + "Excel 12.0 Xml;IMEX=1;HDR=NO;" […]

附加到已创build的excel文件

我有这个Excel文件,目前从我的C#应用​​程序的内容写入其单元格内容: private void button8_Click(object sender, EventArgs e) { Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { MessageBox.Show("Excel is not properly installed!!"); return; } Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlWorkSheet.Cells[1, 1] = comboBox2.Text; xlWorkSheet.Cells[1, 2] = textBox5.Text; xlWorkSheet.Cells[1, 3] = textBox2.Text; xlWorkSheet.Cells[1, 4] = comboBox3.Text; […]

附加到现有的Excel文件

我有写这个function,并保存到一个excel文件。 我现在要做的是附加到相同的现有的Excel文件。这是function: private void button8_Click(object sender, EventArgs e)//creates excel file and writes data to it { Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { MessageBox.Show("Excel is not properly installed!!"); return; } if (System.IO.File.Exists("cross_check.xls")) { } else { Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); IndexProp += […]

(C# – Excel)如何将公式写入每个现有行的新创build列?

假设你有一个excel文件,包含大约100行的数字和行。 我想以编程方式编写一个公式列,为每个现有行添加一堆字段的值。 所以首先,我会为Excel文件创buildOpenfiledialog,findpath,通过OleDb打开它。 明白了这一点。 现在说我有下面的表格: [ 朋友| Money | 钱丢失| 天过去了| 解决 ] Bilbo,50,50,7,*这里公式 Bilso,80,50,7,*这里公式 等等… 问题是,我没有确切的行数,所以我必须枚举OleDb连接find的任何东西。 公式可能类似于=(B2-C2)* D2,但是2也是错误的,它必须是相对于它所在的任何行,即:第3行将是=(B3-C3)* D3。 我不知道如何做到这一点。 我发现直接写入单元格也不会使公式直接处理数字。 ===== 编辑: private Excel.Application Xls; private Excel.Workbooks WBs; private Excel.Workbook WB; private Excel.Worksheet WS; private Excel.Sheets SS; Excel.Range cellsRange = null; Excel.Range columnRange = null; Excel.Range rowRange = null; int numberOfColumns = 0; […]