Tag: c#

datatable到Excel中的C#

在将Datatable导出到Excel时,有些列会落入新行。 我不知道是什么问题。 我的代码如下: string attachment = "attachment; filename=Test.xls"; Response.ClearContent(); Response.AddHeader("content-disposition", attachment); Response.ContentType = "application/vnd.ms-excel"; string tab = ""; foreach (DataColumn dc in transposedTable.Columns) { Response.Write(tab + dc.ColumnName); //tab = "\t"; } Response.Write("\t"); int i; foreach (DataRow dr in transposedTable.Rows) { Response.Write("\n"); tab = ""; for (i = 0; i < transposedTable.Columns.Count; i++) { Response.Write(tab + […]

c#从控制台应用程序创buildExcel工作簿的实例

我有一个Visual Studio解决scheme,它包含两个项目(1)一个C#控制台应用程序; 和(2)一个C#Excel工作簿。 控制台应用程序会创build一些数据,然后将其传入工作簿。 为此,我在Excel工作簿项目中创build了一个方法,该方法从控制台应用程序接收数据。 但是,我无法创buildExcel书籍的实例。 目前我正在尝试: Excel.Application excelApplication = new Excel.Application(); Excel.Workbook excelWorkBook = excelApplication.Workbooks.Add(“MyBook.xls”); 我可以硬编码到xls文件的path,但我不想这样做,因为我宁愿将所有东西都包装成一个.exe 有任何想法吗?

在C#中将XML导出到Excel时缺less标题

在我的应用程序中,我有一个XML文件。我想将XML导出到Excel文件中xls格式。我的问题是当导出头文件丢失。请find下面的代码。 private string ExportTestDatatoExcel(string testDataPath) { Microsoft.Office.Interop.Excel.Application xlApp; Microsoft.Office.Interop.Excel.Workbook xlWorkBook; Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; DataSet ds = new DataSet(); XmlReader xmlFile; int i = 0; int j = 0; xlApp = new Microsoft.Office.Interop.Excel.Application(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xmlFile = XmlReader.Create(testDataPath, new XmlReaderSettings()); ds.ReadXml(xmlFile); for (i = 0; i <= ds.Tables[0].Rows.Count – […]

将excel文件保存在应用程序文件夹中,并使用Windows窗体和C#将内容上传到SQL Server

我正在研究一个简单的应用程序,它需要浏览一个excel文件,并将其上传到SQL服务器,并将该文件复制到应用程序文件夹。 通过使用下面的代码,我可以在网上做到这一点。 但是Winform中没有其他的FileUpload工具。 下面是我试图在Windows窗体中复制的代码。 protected void Upload(object sender, EventArgs e) { //Upload and save the file string excelPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.SaveAs(excelPath); string conString = string.Empty; string extension = Path.GetExtension(FileUpload1.PostedFile.FileName); switch (extension) { case ".xls": //Excel 97-03 conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString; break; case ".xlsx": //Excel 07 or higher conString = ConfigurationManager.ConnectionStrings["Excel07+ConString"].ConnectionString; break; } conString = […]

C#返回值中的工作表Function.CountIfs(…)是一个2 dim对象数组

我的问题是,我想在Excel中使用C#中的WorksheetFunction方法CountIf 。 而且我总是从那个函数中得到一个2个暗淡的对象数组 double[,] zn1 = wsf.CountIfs(cSheet.Range[cSheet.Cells[13, 1], cSheet.Cells[20, 1]], sName + sName); (它只适用于double[,] zn1或var zn1 ,否则我会收到一个对象数组obj[,]不能转换为double或double[] , string[]等的错误。 其中wsf被定义为 Excel.Application xl = new Excel.Application(); Excel.WorksheetFunction wsf = xl.WorksheetFunction; 所以当我运行它时,我得到一个2个暗淡的对象数组,它和Range有相同的长度,我正在寻找,所有的条目都包含相同的数字“-2146826273”。 如果我将数组转换为1 dim double数组,返回值为System.Double[] 。 object[] to = zn1.Cast<object>().ToArray(); object[] res = to as object[]; double[] sRes; sRes = res.OfType<double>().ToArray(); 所有其他WorksheetFunction方法运行良好,没有错误。 所以我的问题是,我如何得到WorksheetFunction方法CountIfs(…) ? 是的,我必须使用WorksheetFunction方法,我不能像string公式一样input。

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;" […]

在Excel VSTO文档级自定义中,如何将“Globals.Sheetn”传递给参数以指定Excel.Worksheet接口?

当我在工作表的部分类文件(例如Sheet8)中编写代码时,如何将“this”类作为期望具有Microsoft.Office.Interop.Excel.Worksheet接口的对象的parameter passing? 下面是一个来自Sheet8.cs文件的例子: public partial class Sheet8 { private void Sheet8_startup(object sender, System.EventArgs e) { var object = new MyCustomClass(this); } } 而我想写的消费代码: class MyCustomClass { public MyCustomClass(Microsoft.Office.Interop.Excel.Worksheet sheet) { // Do something with the sheet } } 问题似乎是“this”总是引用Sheet n对象,并且不能转换为使用自定义代码创build松散耦合接口所必需的接口types。 有什么build议么?

将数据集导出到excelsheet

我是C#中的Excel自动化新手,所以我对此感到困惑。 我已经在数据集中导入了一个excel,并根据我的要求在数据集中进行了一些更新。 现在我想将该数据集导出到该input表,以便可以看到数据表中反映的数据集中所做的更新。 将数据集导出为ex​​cel的最佳方法是什么? 下面是我如何打开Excel表的代码: string sConnection = null; OleDbConnection oleExcelConnection = default(OleDbConnection); sConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\input.xls;Extended Properties=\"Excel 12.0;HDR=No;IMEX=1\""; oleExcelConnection = new OleDbConnection(sConnection); oleExcelConnection.Open(); string sqlquery = "Select * From [c:\input.xls]"; DataSet ds = new DataSet(); OleDbDataAdapter da = new OleDbDataAdapter(sqlquery, oleExcelConnection); da.Fill(ds); System.Data.DataTable dt = ds.Tables[0]; /* 10 to 12 linq queries on dt*/ […]

在打开Excel文件时,将DataTable导出到Excel,而不使用Interop Date Fromat更改

我已经将DataTable导出为ex​​cel文件。 但是,当我打开Excel文件DateTime格式被改变。 当我导出数据date时间字段包含“ 7/21/2016 12:00:00 AM ”值。 而当我打开Excel文件date时间字段显示“ 7/21/2016 0:00 ”值。 但是,当我检查在Excel文件公式栏date时间值显示正确的“ 7/21/2016 12:00:00 AM ”。 有什么我可以做的,以解决这个问题? 我已经使用下面的代码将数据表导出为Excel: HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">"); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + curEventName + ".xls"); HttpContext.Current.Response.Charset = "utf-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250"); ////sets font HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>"); HttpContext.Current.Response.Write("<BR><BR><BR>"); //sets the table […]

如何编写用于VBA Excel的C#UDF的安装程序

基本上,我知道如何在C#中为VBA编写用户定义的函数( 这里 )我想写一个安装程序,它将执行必要的操作,以便在任何Excel文档中自动激活和引用我的* .tlb。 有关registry项或设置的任何信息将会有所帮助 编辑:以下我生成一个MyPj.tbl和一个MyPj.dll(感谢msbuild)。 [InterfaceType(ComInterfaceType.InterfaceIsDual)] [Guid("afb62cb2-dfa8-4f0f-980b-25f96ad93b92")] public interface IGreeting { string SayHelloWorld { get; } } [ClassInterface(ClassInterfaceType.AutoDual)] [Guid("8cd91f78-4e62-4a12-95e0-df82699d4d75")] [ProgId("TestDll.Test")] public class Greeting : IGreeting { public string SayHelloWorld { get { return "Hello, World! "; } } } 通过将生成的.tbl添加到我在Excel中的项目(工具>参考…>浏览),我可以访问Excel VBMacro中的MyPj.Greeting Sub Test() Dim greeter as New MyPj.Greeting MsgBox greeter.SayHelloWorld() End Sub 重点是我不想通过“工具>参考…>浏览”。 我需要做一个安装程序,它会自动在Excel中提供“SayHelloWorld”