Answer the question
In order to leave comments, you need to log in
How to export data to excel in c#
In this way, I transfer the data to strings, then save it to txt.
private string DeviceInformation(string stringIn)
{
StringBuilder StringBuilder1 = new StringBuilder(string.Empty);
try
{
ManagementClass ManagementClass1 = new ManagementClass(stringIn);
//Create a ManagementObjectCollection to loop through
ManagementObjectCollection ManagemenobjCol = ManagementClass1.GetInstances();
//Get the properties in the class
PropertyDataCollection properties = ManagementClass1.Properties;
foreach (ManagementObject obj in ManagemenobjCol)
{
foreach (PropertyData property in properties)
{
try
{
if (property.Name == "Name" ||
property.Name == "Description" ||
property.Name == "Size" ||
property.Name == "Status" ||
property.Name == "ProductName" ||
property.Name == "ProductName" ||
property.Name == "ProductName" ||
property.Name == "FreeSpace" ||
property.Name == "ProviderName" ||
property.Name == "InstallDate" ||
property.Name == "Manufacturer" ||
property.Name == "Version" ||
property.Name == "BIOSVersion" ||
property.Name == "DataWidth" ||
property.Name == "ProcessorType" ||
property.Name == "L2CacheSize" ||
property.Name == "L2CacheSpeed" ||
property.Name == "L3CacheSpeed" ||
property.Name == "LastErrorCode" ||
property.Name == "MaxClockSpeed" ||
property.Name == "L2CacheSize" ||
property.Name == "L2CacheSize" ||
property.Name == "PathName" ||
property.Name == "Network" ||
property.Name == "PrinterStatus" )
{
StringBuilder1.AppendLine(EnToRu(property.Name)+ ": " + obj.Properties[property.Name].Value.ToString());
}
}
catch
{
//Add codes to manage more informations
}
}
StringBuilder1.AppendLine();
}
}
catch
{
//Win 32 Classes Which are not defined on client system
}
return StringBuilder1.ToString();
}
Answer the question
In order to leave comments, you need to log in
Well, you can create an Excel OLE object and populate it. The main thing is to cut down the redrawing and recalculation of formulas from him first, otherwise it will take a long time to do. In addition, it will work with any version and is a completely official solution.
The simplest solution is to save in csv format by separating the columns with ';'.
If you need layout, use any of the libraries :
EPPlus
ExcelLibrary
For a long time, somewhere on the Internet, I found a self-written class for working with Excel. I still use it to this day and I'm happy with it.
Link
Here's how
//создаем экземпляр
MSO.Excel.Excel ex = new MSO.Excel.Excel();
try
{
//проверяем есть ли файл
if (System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Reports\\Naklad.xltx"))
{
ex.OpenBook(AppDomain.CurrentDomain.BaseDirectory + "Reports\\Naklad.xltx");
//пишем значения
ex.SetValue("A1", "Значение1");
ex.SetValue("A2", "Значение1");
ex.Visible = true; //показываем сам Excel
ex.Dispose(); //закрываемся
}
}
catch(Exception) { }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question