A
avivgood
Guest
I have a code that takes a file name from the user, saves it In the roaming folder of the application, and then enters the file name into an excel spreadsheet (then serializing an object into the file):
var Formatter = new BinaryFormatter();
string Project_Name = ProjectName.Text;
if (!RunProjectNameChecks(Project_Name))
{
MessageBox.Show("Project name invalid, please enter valid project name");
return;
}
EmployeeTree NewEmployeeTree = new EmployeeTree();
string FilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "JMange");
Directory.CreateDirectory(FilePath);
try
{
using (var SerializeStream = new FileStream(FilePath + @"\" + Project_Name + ".bin", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
Formatter.Serialize(SerializeStream, NewEmployeeTree);
if (!File.Exists(FilePath + @"\{ProjectData}.xslx"))
{
Excel ex = new Excel();
ex.CreateNewFile();
ex.AddNewSheet();
ex.WriteToCell(0, 0, Project_Name);
ex.SaveAs(FilePath + @"\ProjectData");
ex.CloseConn();
}
else
{
Excel ex = new Excel(FilePath + @"\ProjectData.xslx", 1);
ex.WriteToCell(ex.GetLastRowIndex(), 0, Project_Name);
ex.Save();
ex.CloseConn();
}
}
catch (Exception ex)
{
MessageBox.Show($"Something went wrong. Make sure that the project name is valid and try again. Error: {ex}");
return;
}
Exal class code:
string path = string.Empty;
_Application excel = new _Excel.Application();
Worksheet ws;
Workbook wb;
public Excel(string path, int sheet)
{
this.path = path;
wb = excel.Workbooks.Open(path);
ws = wb.Worksheets[sheet];
}
public Excel() { }
public string GetCell(int row, int collum = 0)
public void WriteToCell(int row, int collum, string str)
{
Addone(ref row, ref collum);
ws.Cells[row, collum].Value2 = str;
}
private void Addone(ref int row, ref int collum)
{
row++;
collum++;
}
public void Save()
{
wb.Save();
}
public void CloseConn()
{
wb.Close();
}
public void SaveAs(string path)
{
wb.SaveAs(path);
}
public void CreateNewFile()
{
this.wb = excel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
this.ws = wb.Worksheets[1];
}
public void AddNewSheet()
{
Worksheet temp = wb.Worksheets.Add(After: ws);
}
public int GetLastRowIndex()
{
_Excel.Range last = ws.Cells.SpecialCells(_Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
_Excel.Range range = ws.get_Range("A1", last);
return last.Row;
}
But I get some errors:
In debug, I have seen that
SerializeStream
Is probably the source of the error:
Name Value Type
◢ ReadTimeout 'SerializeStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
and
Name Value Type
▶ Static members
gives an error, as well, altho its type is not specified.
and
Name Value Type
▶ WriteTimeout 'SerializeStream.WriteTimeout' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
Also prop me a message that the excel file already exist and if I want to replace it (If the file already exist, I wanted the program to write to the next cell in the table, and not recreate the file) I also went to the folder and seen the the exacle table was empty.
what have I done wrong?
Continue reading...
var Formatter = new BinaryFormatter();
string Project_Name = ProjectName.Text;
if (!RunProjectNameChecks(Project_Name))
{
MessageBox.Show("Project name invalid, please enter valid project name");
return;
}
EmployeeTree NewEmployeeTree = new EmployeeTree();
string FilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "JMange");
Directory.CreateDirectory(FilePath);
try
{
using (var SerializeStream = new FileStream(FilePath + @"\" + Project_Name + ".bin", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
Formatter.Serialize(SerializeStream, NewEmployeeTree);
if (!File.Exists(FilePath + @"\{ProjectData}.xslx"))
{
Excel ex = new Excel();
ex.CreateNewFile();
ex.AddNewSheet();
ex.WriteToCell(0, 0, Project_Name);
ex.SaveAs(FilePath + @"\ProjectData");
ex.CloseConn();
}
else
{
Excel ex = new Excel(FilePath + @"\ProjectData.xslx", 1);
ex.WriteToCell(ex.GetLastRowIndex(), 0, Project_Name);
ex.Save();
ex.CloseConn();
}
}
catch (Exception ex)
{
MessageBox.Show($"Something went wrong. Make sure that the project name is valid and try again. Error: {ex}");
return;
}
Exal class code:
string path = string.Empty;
_Application excel = new _Excel.Application();
Worksheet ws;
Workbook wb;
public Excel(string path, int sheet)
{
this.path = path;
wb = excel.Workbooks.Open(path);
ws = wb.Worksheets[sheet];
}
public Excel() { }
public string GetCell(int row, int collum = 0)
public void WriteToCell(int row, int collum, string str)
{
Addone(ref row, ref collum);
ws.Cells[row, collum].Value2 = str;
}
private void Addone(ref int row, ref int collum)
{
row++;
collum++;
}
public void Save()
{
wb.Save();
}
public void CloseConn()
{
wb.Close();
}
public void SaveAs(string path)
{
wb.SaveAs(path);
}
public void CreateNewFile()
{
this.wb = excel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
this.ws = wb.Worksheets[1];
}
public void AddNewSheet()
{
Worksheet temp = wb.Worksheets.Add(After: ws);
}
public int GetLastRowIndex()
{
_Excel.Range last = ws.Cells.SpecialCells(_Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
_Excel.Range range = ws.get_Range("A1", last);
return last.Row;
}
But I get some errors:
In debug, I have seen that
SerializeStream
Is probably the source of the error:
Name Value Type
◢ ReadTimeout 'SerializeStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
and
Name Value Type
▶ Static members
gives an error, as well, altho its type is not specified.
and
Name Value Type
▶ WriteTimeout 'SerializeStream.WriteTimeout' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
Also prop me a message that the excel file already exist and if I want to replace it (If the file already exist, I wanted the program to write to the next cell in the table, and not recreate the file) I also went to the folder and seen the the exacle table was empty.
what have I done wrong?
Continue reading...