Tag: c#

当我下载它们并尝试在openfiledialog中打开它们时,无法读取XLS文件C#

所以我下载了一些excel文件,当我下载这个文件的时候, 这个文件出现了, 现在我觉得一切正常,应该可以正常工作。 所以这里的交易,如果我尝试打开这个文件在我的Visual Studio 2015 openfiledialog 我得到这个错误。 现在当我把excel保存为97-2003 xls文件的时候,它和createbinaryreader一起createbinaryreader 。 如果我把它保存为一个正常的excel格式的xlsx文件,它可以与createopenxmlreader一起createopenxmlreader 。 我怎样才能解决这个问题,所以我不需要去excel文件,并将其保存为excel-workmap(xls或xlsx)? PS:我把这个excel文件放入一个datagridview。

C#Interop定义一个工作表数组

我如何用4个worksheets定义一个数组? 例如,这些是我的worksheets : var wb = (Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook; var wsEvars = wb.Sheets["Evars"]; var wsProps = wb.Sheets["Props"]; var wsEvents = wb.Sheets["Events"]; var wsListVars = wb.Sheets["List Vars"]; 我想把它们包含在一个数组中,如下所示: var sheets = [wsEvars, wsProps, wsEvents, wsListVars];

MVC粘贴HTML到Excel

我有一个html内容,我想导出为ex​​cel文件。 我导出到Excel的function如下所示: public ActionResult ExportToExcel(string htmlToExport) { using (ExcelPackage ep = new ExcelPackage()) { string fileTemplate = "test.xlsx"; using (FileStream stream = new FileStream(HttpContext.Server.MapPath("~/") + ConfigurationManager.AppSettings["templatePath"] + fileTemplate, FileMode.Open)) { ep.Load(stream); } ExcelWorksheet ws = ep.Workbook.Worksheets.First(); Response.AppendHeader("Content-Disposition", fileTemplate); return File(ep.GetAsByteArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); } } 有没有办法将html粘贴到excel文件? 如果我手动这样做,一切都OK。 我想在服务器端做到这一点,并返回粘贴HTML内的Excel文件。 这可能吗?

C#Interop将JSON值添加到工作表

我怎样才能从一个JSON对象获得所有的值,然后一次显示他们的工作表? 这是我的JSON对象的外观: {"report_suites": [ { "rsid": "op-api-docs", "site_title": "API Docs Portal" }, { "rsid": "op-bigideas", "site_title": "Big Ideas" }, { "rsid": "op-education", "site_title": "Education" }, { "rsid": "op-education-dev", "site_title": "Education – Dev" }, { "rsid": "op-global", "site_title": "Global" }, { "rsid": "op-global-dev", "site_title": "Global – Dev" } ]} 预期结果: 这是我试图做到这一点,但它返回以下错误: “System.Collections.Generic.List.AddRange(System.Collections.Generic.IEnumerable)”的最佳重载方法匹配有一些无效参数 dynamic response = JsonConvert.DeserializeObject(responseXml); […]

C#从指定的Excel表导入数据到SQL Server

我有一些C#代码来将Excel导入数据库,但它只能将数据从第一张( Sheet1 )导入到SQL Server数据库中。 我想从Excel文件的第二张( Sheet2 )导入到数据库中的数据。 请修改代码,教我如何做到这一点。 private void button1_Click(object sender, EventArgs e) { OpenFileDialog ope = new OpenFileDialog(); ope.Filter = "Excel Files|*.xls;*.xlsx;*slsm"; if (ope.ShowDialog() == DialogResult.Cancel) return; FileStream stream = new FileStream(ope.FileName, FileMode.Open); IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); DataSet result = excelReader.AsDataSet(); DataClasses1DataContext conn = new DataClasses1DataContext(); foreach(DataTable table in result.Tables) { //Default start […]

获取Excel工作簿的path或文件名

如何在下面的代码中获取工作簿的path或文件名,以便可以获取编辑的工作簿。 我在这里使用了ExpertXLS – 用于.NET的Excel电子表格库 – C#/ VB.NET using ExpertXls; namespace IQC { public class CSFB { public static string GenerateTemplateForCurrentGridView(IQO[] items, string colname, int icol) { /*Some Code here*/ string pathSource = HttpContext.Current.Server.MapPath("~/pdf/ExportTemplate.xlsx"); ExpertXls.ExcelLib.ExcelWorkbook workbook = new ExpertXls.ExcelLib.ExcelWorkbook(@pathSource); workbook.LicenseKey = Inq.Configuration.Settings.Value("ExpertPdfLicenseKey"); ExpertXls.ExcelLib.ExcelWorksheet ws = workbook.Worksheets["ImportTemplate"]; ExpertXls.ExcelLib.ExcelCellStyle Style1 = workbook.Styles.AddStyle("Style1"); Style1.Fill.FillType = ExpertXls.ExcelLib.ExcelCellFillType.SolidFill; Style1.Fill.SolidFillOptions.BackColor = Color.Yellow; […]

如何使用Microsoft.Office.Interop.Exceldynamic增加单元格

我正在尝试开发一个可重复使用的List来优化方法。 使用此示例,“ https://msdn.microsoft.com/en-us/library/dd264733.aspx ” 我看到单元格被单独引用, workSheet.Cells[1, "A"] = "ID Number"; 我希望能够自动填充列名称,并且从左到右dynamic地显示每个单元格。 我可以想象有一个更好的办法,比直接input“A”,“B”,“A1”等工作方式,或者甚至比存储字母表更好的方法。 这是我到目前为止, public static void DownloadExcelFile<T>(List<T> list, string fileName) { Excel.Application excelApp = new Excel.Application(); // Make the object visible. excelApp.Visible = true; excelApp.Workbooks.Add(); Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet; //create column headings foreach (System.ComponentModel.PropertyDescriptor descriptor in System.ComponentModel.TypeDescriptor.GetProperties(list)) { //how can I reference workSheet.Cells by row, […]

Excel阅读器无理由返回空值c#

我用我的代码来读取Excel文件中的值有一个小问题。 我使用一个循环来读取每行的单元格和每个单元格的单元格。 excel文件中的第152行正确读取,但在第153行之后,每个单元格只能得到空值。 这是我的代码: public DataTable parseExcelFile(string path,bool firstLineHeader) { DataTable values = new DataTable("Impayés"); Excel.Application appExcel = new Excel.Application(); Excel.Workbook wb = appExcel.Workbooks.Open(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Excel.Worksheet sheet = (Excel.Worksheet)wb.Sheets["Feuil1"]; Excel.Range excelRange = sheet.UsedRange; bool premiere = firstLineHeader; foreach (Excel.Range row in excelRange.Rows) { if […]

在Excel(2010)电子表格中填充SQL Server数据

我试图从SQL Server中提取数据填充到Excel 2010中。下面的代码工作正常,但困难是,我不创build一个Excel电子表格编程,它是存在的,我通过插件的数据请求在用C#编写的Excel中。 即使我将光标设置为A10单元格,Excel将开始填写第一个单元格中的数据并覆盖头(已存在)。 请帮助解决。 码: OdbcConnection cnn; cnn = new OdbcConnection(azureConn); using (OdbcCommand command = cnn.CreateCommand()) { command.CommandText = "{call sp_Get_Excel_Data(?,?,?,?,?,?,?,?)}"; command.Parameters.AddWithValue("@StartDate", startDate); command.Parameters.AddWithValue("@EndDate", endDate); command.Parameters.AddWithValue("@startTime", startTime); command.Parameters.AddWithValue("@endTime", endTime); command.Parameters.AddWithValue("@smp", smp); command.Parameters.AddWithValue("@Reg", reg); command.Parameters.AddWithValue("@event", events); command.Parameters.AddWithValue("@userId", userId); cnn.Open(); //DataTable OdbcDataAdapter adapter = new OdbcDataAdapter(command); //DataSet DataSet ds = new DataSet(); adapter.Fill(ds); //Cast to DataTable […]

Csharp不能使用oledb读取excel

我正在尝试使用Visual Studio本地主机从.aspx页面读取Excel文件。 我将Excel保存在根文件夹中,但试图读取它时,显示以下exception: Microsoft Office Access数据库引擎无法打开或写入文件“”。 它已经由另一个用户专门打开,或者您需要查看和写入其数据的权限。 我的oledb代码 string connString = ConfigurationManager.ConnectionStrings["xlsx"].ConnectionString; OleDbConnection oledbConn = new OleDbConnection(connString); try { // Open connection oledbConn.Open(); // Create OleDbCommand object and select data from worksheet Sheet1 OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn); // Create new OleDbDataAdapter OleDbDataAdapter oleda = new OleDbDataAdapter(); oleda.SelectCommand = cmd; // […]