private void ExportUserstoCSV(string stringFileName) { // File FileStream1 = new System.IO.File(); StringBuilder StringBuilder1 = new StringBuilder(null); foreach (string string1 in User.StringArrayUesrProperties) { if (StringBuilder1.Length == 0) { StringBuilder1.Append(string1); } StringBuilder1.Append(',' + string1); } StringBuilder1.AppendLine(); foreach (User User1 in Users1) { StringBuilder StringBuilderTemp = new StringBuilder(null); foreach (string string1 in User1.Properties()) { if (StringBuilderTemp.Length == 0) { StringBuilderTemp.Append(string1); } StringBuilderTemp.Append(',' + string1); } // StringBuilder1.AppendLine(); StringBuilder1.AppendLine(StringBuilderTemp.ToString()); } File.WriteAllText(stringFileName, StringBuilder1.ToString(), Encoding.UTF8); MessageBox.Show("Saved Successfully", "Active Directory", MessageBoxButton.OK, MessageBoxImage.Information); }
private void ExportUserstoExcel(string stringFileName) { Excel._Application ExcelApplication; Excel.Workbook ExcelWorkbook; Excel.Worksheet ExcelWorksheet; object objectMisValue = System.Reflection.Missing.Value; Excel.Range ExcelRangeCellinstance; ExcelApplication = new Excel.Application(); ExcelWorkbook = ExcelApplication.Workbooks.Add(objectMisValue); ExcelWorksheet = (Excel.Worksheet)ExcelWorkbook.Worksheets.get_Item(1); ExcelApplication.DisplayAlerts = false; ExcelRangeCellinstance = ExcelWorksheet.get_Range("A1", Type.Missing); int intRow = 1; int intColumn = 1; foreach (string string1 in User.StringArrayUesrProperties) { ExcelWorksheet.Cells[intRow, intColumn] = string1; intColumn++; } intRow++; foreach (User User1 in Users1) { intColumn = 1; foreach (string string1 in User1.Properties()) { ExcelWorksheet.Cells[intRow, intColumn] = string1; intColumn++; } intRow++; } //Highlight first row Excel.Range ExcelRange1 = ExcelWorksheet.get_Range("A1", Type.Missing); ExcelRange1.EntireRow.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black); ExcelRange1.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightSkyBlue); ExcelRange1.EntireRow.Font.Size = 14; ExcelRange1.EntireRow.AutoFit(); //Save Excel ExcelWorkbook.SaveAs(stringFileName, Excel.XlFileFormat.xlWorkbookNormal, objectMisValue, objectMisValue, objectMisValue, objectMisValue, Excel.XlSaveAsAccessMode.xlExclusive, objectMisValue, objectMisValue, objectMisValue, objectMisValue, objectMisValue); ExcelWorkbook.Close(); MessageBox.Show("Saved Successfully", "Active Directory", MessageBoxButton.OK, MessageBoxImage.Information); }
/// <summary> /// Export to CSV file /// </summary> public static void ExporttoCSV() { var dir = @"C:\Zones\"; // folder location if (!Directory.Exists(dir)) // if it doesn't exist, create { Directory.CreateDirectory(dir); } string stringFileName = dir + "UsersLog_" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + "_" + DateTime.Now.Hour + DateTime.Now.Minute + ".csv"; //Save Users StringBuilder StringBuilder1 = new StringBuilder(null); foreach (string string1 in User.StringArrayUesrProperties) { if (StringBuilder1.Length == 0) { StringBuilder1.Append(string1); } StringBuilder1.Append(',' + string1); } StringBuilder1.AppendLine(); foreach (User User1 in Users1) { StringBuilder StringBuilderTemp = new StringBuilder(null); foreach (string string1 in User1.Properties()) { if (StringBuilderTemp.Length == 0) { StringBuilderTemp.Append(string1); } StringBuilderTemp.Append(',' + string1); } // WriteErrorLog(StringBuilderTemp.ToString()); StringBuilder1.AppendLine(StringBuilderTemp.ToString()); } File.WriteAllText(stringFileName, StringBuilder1.ToString(), Encoding.UTF8); }
//Find users in SQL Server who are not in Active directory private void ExportUserstoCSV(string stringFileName, Users Users1) { // File FileStream1 = new System.IO.File(); StringBuilder StringBuilder1 = new StringBuilder(null); foreach (string string1 in User.StringArrayUesrProperties) { if (StringBuilder1.Length == 0) { StringBuilder1.Append(string1); } else { StringBuilder1.Append(',' + string1); } } StringBuilder1.AppendLine(); foreach (User User1 in Users1) { StringBuilder StringBuilderTemp = new StringBuilder(null); foreach (string string1 in User1.Properties()) { if (StringBuilderTemp.Length == 0) { StringBuilderTemp.Append(string1); } else { StringBuilderTemp.Append(',' + string1); } } // StringBuilder1.AppendLine(); StringBuilder1.AppendLine(StringBuilderTemp.ToString()); } System.IO.File.WriteAllText(stringFileName, StringBuilder1.ToString(), Encoding.UTF8); }