用多张表格从c转换到excel
本文关键字:转换 excel 表格 张表格 | 更新日期: 2023-09-27 18:19:36
我有几个不同的字典,其中包含不同类别的信息,我需要将它们全部输出到带有多个电子表格的xls或csv文件中。目前,我必须单独下载特定日期范围的每个excel文件,然后将它们复制粘贴在一起,使它们位于同一文件的不同页面上。有没有办法把它们全部下载到一个文档中?目前,我使用以下代码来输出他们的文件:
writeCsvToStream(
organize.ToDictionary(k => k.Key, v => v.Value as IacTransmittal), writer
);
ms.Seek(0, SeekOrigin.Begin);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Length", ms.Length.ToString());
Response.ContentType = "application/octet-stream";
ms.CopyTo(Response.OutputStream);
Response.End();
其中CCD_ 1仅为单个文件创建文本。
您可以使用一些不同的选项。
- ADO.NET Excel驱动程序-使用此API,您可以使用SQL样式语法将数据填充到Excel文档中。工作簿中的每个工作表都是一个表,工作表中的每个列标题都是该表中的一列等等
以下是一篇关于使用ADO.NET导出到Excel的代码项目文章:http://www.codeproject.com/Articles/567155/Work-with-MS-Excel-and-ADO-NET
ADO.NET方法在多用户web应用程序环境中使用是安全的。
- 使用OpenXML导出数据OpenXML是不同类型文档的架构定义,Excel的更高版本(使用.xlsx、.xlsm等而不仅仅是.xls的版本)对文档使用这种格式。OpenXML模式庞大且有些繁琐,但是您可以用它做任何事情
以下是一篇关于使用OpenXML将数据导出到Excel的代码项目文章:http://www.codeproject.com/Articles/692121/Csharp-Export-data-to-Excel-using-OpenXML-librarie
OpenXML方法在多用户web应用程序环境中使用是安全的。
- 第三种方法是使用COM自动化,这与以编程方式运行Excel桌面应用程序的实例并使用COM控制该实例的操作相同
以下是一篇关于该主题的文章:http://support.microsoft.com/kb/302084
请注意,第三种方法(办公自动化)在多用户web应用程序环境中是不安全的。也就是说,它不应该在服务器上使用,只能从独立的桌面应用程序中使用。
如果您对学习新库持开放态度,我强烈推荐EPPlus。
我在这里做了一些假设,因为你没有发布太多要翻译的代码,但一个使用示例可能看起来像这样:
using OfficeOpenXml;
using OfficeOpenXml.Style;
public static void WriteXlsOutput(Dictionary<string, IacTransmittal> collection) //accepting one dictionary as a parameter
{
using (FileStream outFile = new FileStream("Example.xlsx", FileMode.Create))
{
using (ExcelPackage ePackage = new ExcelPackage(outFile))
{
//group the collection by date property on your class
foreach (IGrouping<DateTime, IacTransmittal> collectionByDate in collection
.OrderBy(i => i.Value.Date.Date)
.GroupBy(i => i.Value.Date.Date)) //assuming the property is named Date, using Date property of DateTIme so we only create new worksheets for individual days
{
ExcelWorksheet eWorksheet = ePackage.Workbook.Worksheets.Add(collectionByDate.Key.Date.ToString("yyyyMMdd")); //add a new worksheet for each unique day
Type iacType = typeof(IacTransmittal);
PropertyInfo[] iacProperties = iacType.GetProperties();
int colCount = iacProperties.Count(); //number of properties determines how many columns we need
//set column headers based on properties on your class
for (int col = 1; col <= colCount; col++)
{
eWorksheet.Cells[1, col].Value = iacProperties[col - 1].Name ; //assign the value of the cell to the name of the property
}
int rowCounter = 2;
foreach (IacTransmittal iacInfo in collectionByDate) //iterate over each instance of this class in this igrouping
{
int interiorColCount = 1;
foreach (PropertyInfo iacProp in iacProperties) //iterate over properties on the class
{
eWorksheet.Cells[rowCounter, interiorColCount].Value = iacProp.GetValue(iacInfo, null); //assign cell values by getting the value of each property in the class
interiorColCount++;
}
rowCounter++;
}
}
ePackage.Save();
}
}
}
感谢您的想法!我最终找到了以下
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook ExcelWorkBook = null;
Excel.Worksheet ExcelWorkSheet = null;
ExcelApp.Visible = true;
ExcelWorkBook = ExcelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
List<string> SheetNames = new List<string>()
{ "Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5", "Sheet6", "Sheet7"};
string [] headers = new string []
{ "Field 1", "Field 2", "Field 3", "Field 4", "Field 5" };
for (int i = 0; i < SheetNames.Count; i++)
ExcelWorkBook.Worksheets.Add(); //Adding New sheet in Excel Workbook
for (int k = 0; k < SheetNames.Count; k++ )
{
int r = 1; // Initialize Excel Row Start Position = 1
ExcelWorkSheet = ExcelWorkBook.Worksheets[k + 1];
//Writing Columns Name in Excel Sheet
for (int col = 1; col < headers.Length + 1; col++)
ExcelWorkSheet.Cells[r, col] = headers[col - 1];
r++;
switch (k)
{
case 0:
foreach (var kvp in Sheet1)
{
ExcelWorkSheet.Cells[r, 1] = kvp.Value.Field1;
ExcelWorkSheet.Cells[r, 2] = kvp.Value.Field2;
ExcelWorkSheet.Cells[r, 3] = kvp.Value.Field3;
ExcelWorkSheet.Cells[r, 4] = kvp.Value.Field4;
ExcelWorkSheet.Cells[r, 5] = kvp.Value.Field5;
r++;
}
break;
}
ExcelWorkSheet.Name = SheetNames[k];//Renaming the ExcelSheets
}
//Activate the first worksheet by default.
((Excel.Worksheet)ExcelApp.ActiveWorkbook.Sheets[1]).Activate();
//Save As the excel file.
ExcelApp.ActiveWorkbook.SaveCopyAs(@"out_My_Book1.xls");