I am currently using Microsoft.Office.Core; and using Excel = Microsoft.Office.Interop.Excel; to export my data from windows form to excel.
When I export my file it always gets overwritten.
Does anyone know of a way to solve it?
xlWorkBook.SaveAs("d:\\csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
MessageBox.Show("Excel file created , you can find the file d:\\csharp-Excel.xls");
You could first check if the file name you want already exists and attach a DateTime at its end if so. Also a '#' before a path gives you opportunity to type your path just like in the explorer (without double '\'):
string MyExportFile = "csharp-Excel";
string FullPath = #"d:\";
if (File.Exists(Path.Combine(FullPath, MyExportFile)))
{
MyExportFile += DateTime.Now + ".xls";
FullPath += MyExportFile;
}
xlWorkBook.SaveAs(FullPath,
Excel.XlFileFormat.xlWorkbookNormal,
misValue, misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive,
misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
Related
i have saw similar issues addressed in other questions but none helped me solve my particular issue although the exception is the same in all of the questions
this is the exception:
System.Runtime.InteropServices.COMException occurred
HResult=0x800A03EC
Message=The file could not be accessed. Try one of the following:
• Make sure the specified folder exists.
• Make sure the folder that contains the file is not read-only.
• Make sure the file name does not contain any of the following characters: < > ? [ ] : | or *
• Make sure the file/path name doesn't contain more than 218 characters.}
this is my code if you could point out my mistake and possibly the solution that would be much of a life saver.
Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("Excel is not properly installed!!");
return;
}
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1][1] = "total to pay";
xlWorkSheet.Cells[2][1] = "due before";
xlWorkSheet.Cells[1][lastUsedRow + 1] = richbox[11 + classnumber ].Text;
xlWorkSheet.Cells[2][lastUsedRow + 1] = Convert.ToString(DateTime.Today.Day) + "-" +Convert.ToString(DateTime.Today.AddMonths(1))+"-" +Convert.ToString(DateTime.Today.Year);
xlApp.DisplayAlerts = false;
xlWorkBook.SaveAs(#"C:\Users\Administrator\Desktop\coach ceazar\clients holds\test.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
I will change the color of some Excel Cells and I use the following function for it:
private void FormatFile(Excel.Borders _borders)
{
_borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
_borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
_borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous;
_borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
_borders.Color = ConsoleColor.Black;
_range = _xlWorkSheet.get_Range("A1:AG100");
_range.EntireColumn.AutoFit();
_range.EntireRow.AutoFit();
Excel.Range colorRange;
colorRange = _xlWorkSheet.get_Range("y1", "ag100");
colorRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGray);
}
It works fine but when the Program will save the file it comes this screen:
Do you have an idea how to change it that I don't become that window ?
I save the file with this function:
internal string SaveFile(string writePath)
{
FormatCells(_xlWorkSheet.Cells.Borders);
string fileName = string.Format("{0}_{1}.xlsx","running_", DateTime.Now.ToString("yyyyMMdd_HHmmss"));
_xlWorkBook.SaveAs(Path.Combine(writePath, fileName), Excel.XlFileFormat.xlWorkbookNormal, misValue,
misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
_xlWorkBook.Close(true, misValue, misValue);
_xlApp.Quit();
ReleaseObject(_xlWorkSheet);
ReleaseObject(_xlWorkBook);
ReleaseObject(_xlApp);
return fileName;
}
Save the file as the newer format xlsx instead of xls.
As covered in greater detail here: Exporting to .xlsx using Microsoft.Office.Interop.Excel SaveAs Error
To programatically save in the XLSX file format you need to use
Excel.XlFileFormat.xlOpenXMLWorkbook
As the file format.
I am writing a very simple program. It runs without any errors. But nothing is written into the Excel file. Am I wrong somewhere?
using Excel = Microsoft.Office.Interop.Excel;
// I already add this reference into my project
public Write()
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(Directory.GetCurrentDirectory() + "\\KKK.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
// This is all I want to write to my Excel file
xlWorkSheet.Cells[1, 1] = "input";
xlWorkBook.SaveAs(Directory.GetCurrentDirectory() + "\\KQ" + ThoiGian(), Excel.XlFileFormat.xlWorkbookDefault, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlApp);
releaseObject(xlWorkBook);
releaseObject(xlWorkSheet);
}
Try
xlWorkSheet.Cells[1, 1].value = "input";
Am trying to put in some data into excel cells. I get HResult rnge exception. below is the code. And also am not able to wrap the text in the cell [1,B] . am a fresher in using Office apps and not able to find a solution .
myExcelApp = new Excel.Application();
myExcelApp.Visible = true;
myExcelWorkbooks = myExcelApp.Workbooks;
String fileName1 = "D:\\book1.xlsx";
myExcelWorkbook = myExcelWorkbooks.Open(fileName1, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
Excel.Worksheet myExcelWorksheet = (Excel.Worksheet)myExcelWorkbook.ActiveSheet;
String cellFormulaAsString = myExcelWorksheet.get_Range("A2", misValue).Formula.ToString();
Microsoft.Office.Interop.Excel.Range range = myExcelWorksheet.UsedRange;
myExcelWorksheet.Cells[1, "A"] = text;
myExcelWorksheet.Cells[1, "B"] = commentText;
// myExcelWorksheet.Cells[1, "C"] = OccuranceList;
Excel.Range r = myExcelWorksheet.get_Range("B7", "A");
r.EntireRow.AutoFit();
Excel.Range r = myExcelWorksheet.get_Range("B7", "A");
You are missing a row number for Cell A
Something like this?
Excel.Range r = myExcelWorksheet.get_Range("B7", "A1");
FOLLOWUP
I just want to wrap text in cell[1,"B"] as the string is very large. – user1665707 59 mins ago
I dont get any error now. But the text wrapping is not happening. a part of the text is not visible. – user1665707 17 mins ago
Yes, it will not autofit. See this link for the reason.
I am trying to insert text files into excel cell using Querytables.Add; no error, but the worksheet is empty. except for the single cell manipulation using Value2 property.
I already using macro to record the object used.
Can you help me on this(I am using vs2008, C# , excel 2003 and 2007; both shown empty cell).
Below is my code; thanks for your help
Application application = new ApplicationClass();
try
{
object misValue = Missing.Value;
wbDoc = application.Workbooks.Open(flnmDoc, misValue, misValue, misValue, misValue, misValue, misValue,
misValue, misValue, misValue, misValue, misValue, misValue, misValue,
misValue);
wsRefDocBudgetOwner = (Worksheet)wbDoc.Worksheets[2];
Range lRange = wsRefDocBudgetOwner.get_Range("B2", "B25");
var temp2 = wsRefDocBudgetOwner.QueryTables;
var temp = temp2.Add(#"TEXT;d:\temp\config ssas.txt", lRange, Type.Missing);
//temp.RefreshStyle = XlCellInsertionMode.xlInsertDeleteCells;
//temp.RefreshOnFileOpen = true;
wsRefDocBudgetOwner.get_Range("B1", "B1").Value2 = "Lgfdgast adsffdafadfads";
wbDoc.Save();
//wbDoc.SaveAs(flnmDoc2, misValue, misValue, misValue, misValue, misValue, XlSaveAsAccessMode.xlExclusive,
// misValue, misValue, misValue, misValue, misValue);
wbDoc.Close(Missing.Value, Missing.Value, Missing.Value);
}
finally
{
application.Quit();
}
I found it;
it is the RefreshStyle property. It should be set to xlInsertEntierRows.
temp.RefreshStyle = XlCellInsertionMode.xlInsertEntireRows;
as shown in http://support.microsoft.com/kb/306023