• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

Problem to save Excel file

Hi,
Using these
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using Microsoft.Office.Interop;
using Excel = Microsoft.Office.Interop.Excel;
...
            Excel.Application xlApp = new
            Microsoft.Office.Interop.Excel.Application();
            workbook = xlApp.Workbooks.Add(misValue);

            workbook = xlApp.Workbooks.Add(misValue);
            worksheet = (Excel.Worksheet)workbook.Worksheets.get_Item(1);

            ...
            workbook.SaveAs("c://" + hkb + ".xls", Excel.XlFileFormat.xlExcel8, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            workbook.Close(true, misValue, misValue);
            xlApp.Quit();

Open in new window


on VS 2013, I get this error
https://dl.dropboxusercontent.com/u/40211031/tt119.png

when running the project. why?
0
HuaMin Chen
Asked:
HuaMin Chen
  • 3
  • 2
  • 2
1 Solution
 
Haris DulicCommented:
Hi,

it seams the issue is with file name can you try:

dim file_name as string

file_name= "c://" + hkb + ".xls"

 MessageBox.Show (file_name, "Test",MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);

workbook.SaveAs(file_name, Excel.XlFileFormat.xlExcel8, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

Open in new window

0
 
HuaMin ChenProblem resolverAuthor Commented:
Yes, using these codes

                fl_nm = "c://" + hkb + ".xls";
                workbook.SaveAs(fl_nm, Excel.XlFileFormat.xlExcel8, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                workbook.Close(true, misValue, misValue);
                xlApp.Quit();

Open in new window

I still get this exception

Error encountered when saving Excel file 'c://HKB.xls'   at Microsoft.Office.Int
erop.Excel._Workbook.SaveAs(Object Filename, Object FileFormat, Object Password,
 Object WriteResPassword, Object ReadOnlyRecommended, Object CreateBackup, XlSav
eAsAccessMode AccessMode, Object ConflictResolution, Object AddToMru, Object Tex
tCodepage, Object TextVisualLayout, Object Local)
   at FX_Pos_rep.Program.Main(String[] args)

Open in new window

0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Probably a user permissions problem - saving to the root directory C, try creating a sub folder and saving to that instead.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
HuaMin ChenProblem resolverAuthor Commented:
It is saving to root directory now. And I have no such problem to save to root dir, using other application.
0
 
Haris DulicCommented:
Hi,

check the rights on folders "C:\Windows\SysWOW64\config\systemprofile" and/or   "C:\Windows\System32\config\systemprofile" and set it to Full control as described here
0
 
HuaMin ChenProblem resolverAuthor Commented:
Sorry, I have a problem to set

C:\Windows\SysWOW64\config\systemprofile

to Full control.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
>>And I have no such problem to save to root dir, using other application.

OK, so you don't want to test something simple.  (Look, if it is not this problem it takes you a minute to confirm that - if it is this problem you waste hours).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now