I'm trying to copy data from textboxes and set them on specific cells on Excel file In which have 2 sheets and sheet 1 named as "PM" and sheet 2 named as "4-10-2018" the process successfully done but on the second sheet not display data properly.
I checked my code many times but found nothing to fix this issue.
Code:
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Worksheet xlsht = new Microsoft.Office.Interop.Excel.Worksheet();
string path = #"D:\test.xlsx";
xlsht = xlApp.Application.Workbooks.Open(path).Worksheets["PM"];
xlsht.Cells[11, 2] = UserNameTxt.Text + "#rasatop.com";
xlsht.Cells[11, 4] = UserNameTxt.Text;
xlsht.Cells[14, 2] = SerialTxt.Text;
xlsht.Cells[16, 2] = WLANMacTxt.Text;
xlsht.Cells[16, 3] = LANMacTxt.Text;
xlsht.Cells[16, 4] = IPTxt.Text;
xlsht.Cells[14, 5] = ComputerTxt.Text;
xlsht.Cells[16, 5] = BarcodeTxt.Text;
xlsht.Cells[18, 5] = CPUTxt.Text.Substring(0, 26);
xlsht.Cells[18, 4] = VGATxt.Text;
xlsht.Cells[18, 3] = RAMTxt.Text;
xlsht.Cells[27, 4] = OSTxt.Text;
xlsht.Cells[5, 4] = System.DateTime.Today;
xlsht.Cells[26, 4] = System.DateTime.Today;
xlsht.Cells[9, 5] = System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;
xlApp.Visible = true;
Microsoft.Office.Interop.Excel.Worksheet xlsht2 = new Microsoft.Office.Interop.Excel.Worksheet();
xlsht2 = xlApp.Application.Workbooks.Open(path).Worksheets["4-10-2018"];
xlsht2.Cells[4, 2] = System.DateTime.Today;
xlsht2.Cells[6, 2] = UserNameTxt.ToString();
xlsht2.Cells[6, 4] = ComputerTxt.ToString();
xlsht2.Cells[6, 5] = BarcodeTxt;
And after executing the code run successfully but data displayed as below in Screenshot:
Display Error:
In some places you are putting calling the .ToString() method of the TextBox control into your spreadsheet cell rather than reading its UserNameTxt.Text property which will contain the value.
Here is your problem UserNameTxt.ToString(). You have to getText property for TextBox value. Convert UserNameTxt.ToString()to UserNameTxt.Text
Related
I am using this code to create an excel instance which is used for making a report
excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = true;
excel.DisplayAlerts = false;
worKbooK = excel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
worKsheeT = worKbooK.Worksheets[1]; ;
worKsheeT.Name = "Report";
iCountW = 2;
worKsheeT.Cells[1, 1] = "S.No";
worKsheeT.Cells[1, 2] = "Weld Stud/Nut Name";
worKsheeT.Cells[1, 3] = "Weld Spot Clearance Status";
worKsheeT.Cells[1, 4] = "Weld Spot Name";
worKsheeT.Cells[1, 5] = "Coord-X";
worKsheeT.Cells[1, 6] = "Coord-Y";
worKsheeT.Cells[1, 7] = "Coord-Z";
// Some Code
// Some more code
// No more code
After updating the data rows of this report I am using the below code for saving the excel workbook which is not letting me save the workbook.
string filename = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory) + "\\" + "Trial_StudNut.xlsx";
if (!System.IO.File.Exists(filename))
{
worKbooK.SaveAs(filename, Excel.XlSaveAsAccessMode.xlNoChange);
}
Can someone help me with what am I missing to avoid exceptions while saving the workbook?
Edit: I am getting the following
(HRESULT: 0x800A03EC Error while saving Excel file)
The code is getting inside the condition and then falling into the exception.
Nowadays am working on a c# desktop application and in this application after a transaction completed that transaction export in excel but I want to make an excel font size bold I don't know how to do it please help me in this scenario.
Thanks.
here is my code
//creating Excel Application
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
//creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
//creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
//see the excel sheet behind the program
app.Visible = true;
//get the reference of first sheet. By default its name is Sheet1.
//store its reference to worksheet
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
//changing the name of active sheet
worksheet.Name = "Exported from gridview";
//storing header part in Excel
//
worksheet.Cells[1, 1] = "Customer Name";
worksheet.Cells[1, 2] = "S/D/W";
worksheet.Cells[1, 3] = "NIC";
worksheet.Cells[1, 4] = "Postal Address";
worksheet.Cells[1, 5] = "Flat No";
worksheet.Cells[1, 6] = "Type";
worksheet.Cells[3, 1] = "Floor";
worksheet.Cells[3, 2] = "Block";
worksheet.Cells[3, 3] = "Size";
worksheet.Cells[3, 5] = "File No";
worksheet.Cells[3, 6] = "Nominee";
worksheet.Cells[5, 1] = "Relation";
worksheet.Cells[5, 2] = "nominee_NIC";
worksheet.Cells[5, 3] = "Contact_NIC";
worksheet.Cells[5, 4] = "Status";
worksheet.Cells[5, 5] = "Booking Station";
worksheet.Cells[5, 6] = "Booked By";
worksheet.Cells[7, 1] = "Cost";
worksheet.Cells[7, 2] = "Discount";
worksheet.Cells[7, 3] = "Total";
workbook.SaveAs("D:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
worksheet.Cells[1, 1].Font.Bold = true; should do the job
I am trying to export an SQL Dataset to an excel document.
At certain points when outputting the date fields the excel cells are being formatted at certain points as date cells and some as general.
This is causing the dates to display sometime in the format that I want, UK style day/month/year, in the American style month/day/year.
If it was just one way that would be fine because the report would be at least consistent. However the fact that the code will sometime output the correct format and sometimes won't is very annoying
My code so far:
private void doReport(DataSet myDS)
{
Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
int i;
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "Report produced on the " + System.DateTime.Now.ToString("dd/MM/yyyy");
xlWorkSheet.Cells[3, 1] = "Project Type";
xlWorkSheet.Cells[3, 2] = "Client";
xlWorkSheet.Cells[3, 3] = "Date Started";
xlWorkSheet.Cells[3, 4] = "Target Date";
xlWorkSheet.Cells[3, 5] = "Date Completed";
xlWorkSheet.Cells[3, 6] = "Stage Target";
xlWorkSheet.Cells[3, 7] = "Stage";
xlWorkSheet.Cells[3, 8] = "Assigned To";
xlWorkSheet.Cells[3, 9] = "Notes";
i = 4;
foreach (DataRow row in myDS.Tables[0].Rows)
{
xlWorkSheet.Cells[i, 1] = row["strProjectDescription"];
xlWorkSheet.Cells[i, 2] = row["strClient"];
DateTime dteProjStart = (DateTime)row["dteProjectStart"];
xlWorkSheet.Cells[i, 3] = dteProjStart.ToString("dd/MM/yyyy");
DateTime dteProjTarget = (DateTime)row["dteProjectTarget"];
xlWorkSheet.Cells[i, 4] = dteProjTarget.ToString("dd/MM/yyyy");
if (rbViewProjectsCompletedVal.Checked)
{
DateTime dteProjFin = (DateTime)row["dteProjectFinished"];
xlWorkSheet.Cells[i, 5] = dteProjFin.ToString("dd/MM/yyyy");
}
if (row.IsNull("dteProjectStageTarget") == false)
{
DateTime dteProjStageTar = (DateTime)row["dteProjectStageTarget"];
xlWorkSheet.Cells[i, 6] = dteProjStageTar.ToString("dd/MM/yyyy");
}
xlWorkSheet.Cells[i, 7] = row["strProjectStageDescription"];
xlWorkSheet.Cells[i, 8] = row["initials"];
xlWorkSheet.Cells[i, 9] = csSQL.runSQL("select strProjectNotes from tbl_AdminSupport_Projects where intASProjectID = " + row["intASProjectID"], "Management").Tables[0].Rows[0]["strProjectNotes"];
i++;
}
xlWorkSheet.Columns.AutoFit();
xlWorkSheet.Rows.AutoFit();
xlApp.Visible = true;
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
Dataset structure:
Any item prefixed with str is a nvarchar(max), dte is a date
I Excel File with 2 sheets, First sheet named as "PM" and second Sheet named as "km" when i try to enter data to sheet 1 from textboxes it successfully done but when i tried to enter data in sheet 2.. sheet 1 back to its original state(Empty)..
I checked the code i made at first 1 worksheet "xlsht" for both sheets but data not updated on both sheets then i made 2 functions with 2 worksheets but the problem is same, any idea why?
My Code:
protected void PM_Sheet1()
{
try
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Worksheet xlsht = new Worksheet();
string path = #"D:\test.xlsx";
xlsht = xlApp.Application.Workbooks.Open(path).Worksheets["PM"];
xlsht.Cells[11, 2] = UserNameTxt.Text + "#rasatop.com";
xlsht.Cells[11, 4] = UserNameTxt.Text;
xlsht.Cells[14, 2] = SerialTxt.Text;
xlsht.Cells[16, 2] = WLANMacTxt.Text;
xlsht.Cells[16, 3] = LANMacTxt.Text;
xlsht.Cells[16, 4] = IPTxt.Text;
xlsht.Cells[14, 5] = ComputerTxt.Text;
xlsht.Cells[16, 5] = BarcodeTxt.Text;
xlsht.Cells[18, 5] = CPUTxt.Text.Substring(0, 26);
xlsht.Cells[18, 4] = VGATxt.Text;
xlsht.Cells[18, 3] = RAMTxt.Text;
xlsht.Cells[27, 4] = OSTxt.Text;
xlsht.Cells[5, 4] = System.DateTime.Today;
xlsht.Cells[26, 4] = System.DateTime.Today;
xlsht.Cells[9, 5] = System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;
xlsht.SaveAs(#"D:\test1.xlsx");
}
catch (Exception)
{
MessageBox.Show(#"Make Sure test.xlsx file in D:\ Drive");
}
}
protected void PM_Sheet2()
{
try
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Worksheet xlsht = new Worksheet();
string path = #"D:\test1.xlsx";
xlsht = xlApp.Application.Workbooks.Open(path).Worksheets["km"];
xlsht.Cells[4, 2] = System.DateTime.Today;
xlsht.Cells[6, 2] = UserNameTxt.Text;
xlsht.Cells[6, 4] = ComputerTxt.Text;
xlsht.Cells[6, 5] = BarcodeTxt.Text;
xlsht.SaveAs(#"D:\" + ComputerTxt.Text + ".xlsx");
xlApp.Visible = true;
}
catch (Exception)
{
MessageBox.Show("Error Occured Cannot Save Sheet 2");
}
}
despite documentation, Worksheet.SaveAs() would lead to an error, so you have to use Workbook.SaveAs()
You're also opening two different Excel instances where you need only one and apparently leaving both them running!
Finally, you'd better open an Excel instance for all operations and hence distributing necessary code at Form class level (declarations), button1_Click() level (Excel and Workbook initialization), helper methods (Worksheet initializations)
As follows:
using System;
using System.Windows.Forms;
using Worksheet = Microsoft.Office.Interop.Excel.Worksheet;
using Workbook = Microsoft.Office.Interop.Excel.Workbook;
using MsExcelApp = Microsoft.Office.Interop.Excel.Application;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
MsExcelApp xlApp =;
Workbook xlWb;
Worksheet xlSht;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string path = #"D:\test.xlsx";
xlApp = new MsExcelApp();
try
{
xlWb = xlApp.Application.Workbooks.Open(path);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
PM_Sheet1();
PM_Sheet2();
try
{
xlWb.SaveAs(#"D:\" + ComputerTxt.Text + ".xlsx");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
xlApp.Quit();
}
protected void PM_Sheet1()
{
try
{
xlSht = xlWb.Worksheets["PM"];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
xlSht.Cells[11, 2] = UserNameTxt.Text + "#rasatop.com";
xlSht.Cells[11, 4] = UserNameTxt.Text;
xlSht.Cells[14, 2] = SerialTxt.Text;
xlSht.Cells[16, 2] = WLANMacTxt.Text;
xlSht.Cells[16, 3] = LANMacTxt.Text;
xlSht.Cells[16, 4] = IPTxt.Text;
xlSht.Cells[14, 5] = ComputerTxt.Text;
xlSht.Cells[16, 5] = BarcodeTxt.Text;
xlSht.Cells[18, 5] = CPUTxt.Text.Substring(0, 26);
xlsht.Cells[18, 4] = VGATxt.Text;
xlsht.Cells[18, 3] = RAMTxt.Text;
xlsht.Cells[27, 4] = OSTxt.Text;
xlSht.Cells[5, 4] = System.DateTime.Today;
xlSht.Cells[26, 4] = System.DateTime.Today;
xlSht.Cells[9, 5] = System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;
}
protected void PM_Sheet2()
{
try
{
xlSht = xlWb.Worksheets["km"];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
xlSht.Cells[4, 2] = System.DateTime.Today;
xlSht.Cells[6, 2] = UserNameTxt.Text;
xlSht.Cells[6, 4] = ComputerTxt.Text;
xlSht.Cells[6, 5] = BarcodeTxt.Text;
}
}
}
I am working with a windows form application using c#. This program is able to read the data from the sql server and then and export to excel. I got this part working. However, I want to be able to generate a list of random password in a column according to how many rows of item I have in my excel. Let say my password column is column 'M' (password) and let say if the excel file has ten items (10 rows), then column 'M' will display 10 random generated password. If there is 5 item, then column 'M' will display 5 random password. I am thinking to use for loop to do this. But I have not idea where to start. Help will be appreciated. I am using Microsoft.interop.excel library 15.
Here is the password pattern that I want
abcd001
abcd002
abcd003
abcd004
abcd005
In the excel let say if I have 10 rows, then 10 column M it will display a list of password
A B C ..... M
1 abcd001
2 abcd002
3 abcd003
4 abcd004
5 abcd005
6 abcd006
7 abcd007
8 abcd008
9 abcd009
10 abcd010
Here is the part of my code to create excel file
private void exportToExcel(){
object missing = Type.Missing;
Excel.Application xlApp = new Excel.Application();
xlApp.Visible = false;
Excel.Workbook xlwb = xlApp.Workbooks.Add(missing);
Excel.Worksheet xlEmployeeDetail= xlwb.ActiveSheet as Excel.Worksheet;
xlEmployeeDetail.Name = "Employee Detail";
xlEmployeeDetail.Cells[1, 1] = "Employee Id";
xlEmployeeDetail.Cells[1, 2] = "First Name";
xlEmployeeDetail.Cells[1, 3] = "Last Name";
xlEmployeeDetail.Cells[1, 4] = "Nick Name";
xlEmployeeDetail.Cells[1, 5] = "Email";
xlEmployeeDetail.Cells[1, 6] = "Entry Year";
xlEmployeeDetail.Cells[1, 7] = "Leave Year";
.........
xlEmployeeDetail.Cells[1, 12] = "User Login ID";
xlEmployeeDetail.Cells[1, 13] = "Password";
while (dr.Read())
{
i++;
int e_id = dr.GetInt32(1);
string f_Name = dr.GetString(5);
string l_Name = dr.GetString(6);
string n_Name = dr.GetString(7);
string e_email = dr.GetString(8);
int e_Year = dr.GetInt32(9);
//int l_Year = dr.GetInt32(10);
xlEmployeeDetail.Cells[i, 1] = t_id;
xlEmployeeDetail.Cells[i, 2] = f_Name;
xlEmployeeDetail.Cells[i, 3] = l_Name;
xlEmployeeDetail.Cells[i, 4] = zh_Name;
xlEmployeeDetail.Cells[i, 5] = t_email;
xlEmployeeDetail.Cells[i, 6] = e_Year;
.....
}
dr.Close();
}
Firstly, create a mutual prefix for your password before the while loop.
string passwordPrefix = "abcd";
Then, inside the loop, add the prefix to a properly formatted i.ToString() (using String.Format). You might want to create the format dynamically, because you might need 000 or 0000 etc., depending on the number of rows.