我如何在不需要安装Excel的计算机上使用C#创建Excel电子表格?
我如何在不需要安装Excel的计算机上使用C#创建Excel电子表格?
//Closed XML
var workbook = new XLWorkbook(sUrlFile); // load the existing excel file
var worksheet = workbook.Worksheets.Worksheet(1);
worksheet.Cell("A15").SetValue("");
workbook.Save();
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", "");
Aspose
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;
当从像SQL Server这样的数据库创建Excel表格时,特别容易使用...
之前我使用NPOI创建了一个DLL库。使用起来非常简单:
IList<DummyPerson> dummyPeople = new List<DummyPerson>();
//Add data to dummyPeople...
IExportEngine engine = new ExcelExportEngine();
engine.AddData(dummyPeople);
MemoryStream memory = engine.Export();
您可以在这里阅读更多相关编程内容。
顺便提一下,它是100%的开源。请自由使用、编辑和分享;)
wb.SaveAs(filename, 51, System.Reflection.Missing.Value,
System.Reflection.Missing.Value, false, false, 1,1, true,
System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value)
try
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Excel Documents (*.xls)|*.xls";
saveFileDialog1.FileName = "Employee Details.xls";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string fname = saveFileDialog1.FileName;
StreamWriter wr = new StreamWriter(fname);
for (int i = 0; i <DataTable.Columns.Count; i++)
{
wr.Write(DataTable.Columns[i].ToString().ToUpper() + "\t");
}
wr.WriteLine();
//write rows to excel file
for (int i = 0; i < (DataTable.Rows.Count); i++)
{
for (int j = 0; j < DataTable.Columns.Count; j++)
{
if (DataTable.Rows[i][j] != null)
{
wr.Write(Convert.ToString(getallData.Rows[i][j]) + "\t");
}
else
{
wr.Write("\t");
}
}
//go to next line
wr.WriteLine();
}
//close file
wr.Close();
}
}
catch (Exception)
{
MessageBox.Show("Error Create Excel Sheet!");
}
using var workbook = new XSSFWorkbook();
var sheet = workbook.CreateSheet("Sheet1");
int rowIndex = 0;
var row = sheet.CreateRow(rowIndex++);
int cellIndex = 0;
row.CreateCell(cellIndex++).SetCellValue("ID");
row.CreateCell(cellIndex++).SetCellValue("Name");
var row = sheet.CreateRow(rowIndex++);
cellIndex = 0;
row.CreateCell(cellIndex++).SetCellValue("1");
row.CreateCell(cellIndex++).SetCellValue("One");
var row = sheet.CreateRow(rowIndex++);
cellIndex = 0;
row.CreateCell(cellIndex++).SetCellValue("2");
row.CreateCell(cellIndex++).SetCellValue("Two");
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
string xlsxPath = Path.Combine(path, $"vdfgdfg.xlsx");
using var fileStream = new FileStream(xlsxPath, FileMode.Create, FileAccess.Write);
workbook.Write(fileStream);
看这里,无需第三方库,你可以使用以下方法将 DataTable 数据直接导出为 Excel 文件。
var dt = "your code for getting data into datatable";
Response.ClearContent();
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", DateTime.Now.ToString("yyyy-MM-dd")));
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
foreach (DataColumn dataColumn in dt.Columns)
{
Response.Write(tab + dataColumn.ColumnName);
tab = "\t";
}
Response.Write("\n");
int i;
foreach (DataRow dataRow in dt.Rows)
{
tab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
Response.Write(tab + dataRow[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();
for (int i = 0; i < TotalFile; i++)
{
Contact.Clear();
if (innerloop == SplitSize)
{
for (int j = 0; j < SplitSize; j++)
{
string strContact = DSt.Tables[0].Rows[i * SplitSize + j][0].ToString();
Contact.Add(strContact);
}
string strExcel = strFileName + "_" + i.ToString() + ".xlsx";
File.WriteAllLines(strExcel, Contact.ToArray());
}
}
也请参考链接
http://dotnet-magic.blogspot.in/2011/10/createformat-excel-file-from-cnet.html
Contact
类,而你并没有告诉我们它是什么。如果它适用于xls,则很有可能你实际上正在编写HTML,而不是真正的Excel文件。而且你的链接正在使用interop,正如上面提到的,不应该在服务器端使用,并且在填充大表格时可能会变慢。 - Rup