如何使用C#创建Excel电子表格而不需要在运行代码的计算机上安装Excel?
当前回答
IKVM+波伊
或者,你可以使用Interop。。。
其他回答
我成功地使用了以下开源项目:
用于OOXML格式的ExcelPackage(Office 2007)XLS格式的NPOI(Office 2003)。NPOI 2.0(Beta版)也支持XLSX。
看看我的博客帖子:
在C中创建Excel电子表格.XLS和.XLSX#
带有Excel表格和动态图表的NPOI
如何在OneDrive上使用C#创建Excel(.xslx)文件而不安装Microsoft Office
Microsoft Graph API提供文件和Excel API,用于为企业和消费者帐户创建和修改存储在OneDrive中的Excel文件。Microsoft.Graph NuGet包提供了许多用于使用File和Excel API的接口。
{
Name = "myExcelFile.xslx",
File = new Microsoft.Graph.File()
};
// Create an empty file in the user's OneDrive.
var excelWorkbookDriveItem = await graphClient.Me.Drive.Root.Children.Request().AddAsync(excelWorkbook);
// Add the contents of a template Excel file.
DriveItem excelDriveItem;
using (Stream ms = ResourceHelper.GetResourceAsStream(ResourceHelper.ExcelTestResource))
{
//Upload content to the file. ExcelTestResource is an empty template Excel file.
//https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/item_uploadcontent
excelDriveItem = await graphClient.Me.Drive.Items[excelWorkbookDriveItem.Id].Content.Request().PutAsync<DriveItem>(ms);
}
此时,您现在已经在用户(企业或消费者)或组的OneDrive中创建了一个Excel文件。现在,您可以使用Excel API对Excel文件进行更改,而无需使用Excel,也无需了解Excel XML格式。
只想添加另一个引用到直接解决您问题的第三方解决方案:http://www.officewriter.com
(免责声明:我为制作OfficeWriter的公司SoftArtisans工作)
一个非常轻量级的选项可能是使用HTML表。只需在文件中创建head、body和table标记,然后将其另存为扩展名为.xls的文件。您可以使用Microsoft特定的属性来设置输出样式,包括公式。
我意识到,您可能没有在web应用程序中对此进行编码,但这里有一个通过HTML表组成Excel文件的示例。如果您正在编写控制台应用程序、桌面应用程序或服务,则可以使用此技术。
在我的项目中,我使用一些.net库来提取Excel文件(.xls和.xlsx)
为了导出数据,我经常使用rdlc。
要修改我使用的excel文件(尝试设置空白单元格A15时的示例代码):
关闭的XML
//Closed XML
var workbook = new XLWorkbook(sUrlFile); // load the existing excel file
var worksheet = workbook.Worksheets.Worksheet(1);
worksheet.Cell("A15").SetValue("");
workbook.Save();
铁杆XL
string sUrlFile = "G:\\ReportAmortizedDetail.xls";
WorkBook workbook = WorkBook.Load(sUrlFile);
WorkSheet sheet = workbook.WorkSheets.First();
//Select cells easily in Excel notation and return the calculated value
sheet["A15"].First().Value = "";
sheet["A15"].First().FormatString = "";
workbook.Save();
workbook.Close();
workbook = null;
SpireXLS(当我尝试时,库会打印附加页,以提供我们使用试用库的信息
string sUrlFile = "G:\\ReportAmortizedDetail.xls";
Workbook workbook = new Workbook();
workbook.LoadFromFile(sUrlFile);
//Get the 1st sheet
Worksheet sheet = workbook.Worksheets[0];
//Specify the cell range
CellRange range = sheet.Range["A15"];
//Find all matched text in the range
CellRange[] cells = range.FindAllString("hi", false, false);
//Replace text
foreach (CellRange cell in range)
{
cell.Text = "";
}
//Save
workbook.Save();
Jet Oledb公司
//ExcelTool Class
public static int ExcelUpdateSheets(string path, string sWorksheetName, string sCellLocation, string sValue)
{
int iResult = -99;
String sConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO'";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("UPDATE [" + sWorksheetName + "$" + sCellLocation + "] SET F1=" + UtilityClass.ValueSQL(sValue), objConn);
objCmdSelect.ExecuteNonQuery();
objConn.Close();
return iResult;
}
用法:
ExcelTool.ExcelUpdateSheets(sUrlFile, "ReportAmortizedDetail", "A15:A15", "");
天冬氨酸
var workbook = new Aspose.Cells.Workbook(sUrlFile);
// access first (default) worksheet
var sheet = workbook.Worksheets[0];
// access CellsCollection of first worksheet
var cells = sheet.Cells;
// write HelloWorld to cells A1
cells["A15"].Value = "";
// save spreadsheet to disc
workbook.Save(sUrlFile);
workbook.Dispose();
workbook = null;
推荐文章
- Selenium c# WebDriver:等待元素出现
- 我如何添加双引号的字符串,是在一个变量?
- 如何创建数组。包含不区分大小写的字符串数组?
- 检查字符串是否包含字符串列表中的元素
- 最好的方法在asp.net强制https为整个网站?
- 将字符串转换为System.IO.Stream
- 如何从枚举中选择一个随机值?
- 驻留在App_Code中的类不可访问
- 在链式LINQ扩展方法调用中等价于'let'关键字的代码
- dynamic (c# 4)和var之间的区别是什么?
- Visual Studio: ContextSwitchDeadlock
- 返回文件在ASP。Net Core Web API
- 自定义HttpClient请求头
- 如果我使用OWIN Startup.cs类并将所有配置移动到那里,我是否需要一个Global.asax.cs文件?
- VS2013外部构建错误"error MSB4019: The imported project <path> was not found"