Excel文件扩展名问题 [英] excel file extension problem
本文介绍了Excel文件扩展名问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好...
我有一个Windows应用程序,我必须通过savefiledialog工具将用户输入的数据保存在excel表格中...但是当我尝试保存它时,它会正确保存...但是当我转到我保存的位置时它,n打开文件,它仅打开一次,下次我尝试打开它,错误提示文件格式或扩展名不正确... n,所以它无法打开...
请帮助...
这是我的代码.
Hello...
I have a windows app where i have to save the data entered by the user in excel sheet via savefiledialog tool...but when i try to save it,it gets saved properly...but when i go to the location where i saved it,n open the file,it opens only once,next time i try to open it,error is displayed that file format or extension is incorrect...n so it cant open...
plz help...
here is my code..
Excell.Application xlApp;
Excell.Workbook xlWorkBook;
Excell.Worksheet xlWorkSheet;
Excell.Range range;
string str = "";
int rCnt = 0;
int cCnt = 0;
xlApp = new Excell.ApplicationClass();
object BlankValue = System.Reflection.Missing.Value;
string docpath = Environment.GetFolderPath(Environment.SpecialFolder
.MyDocuments);
string path1 = string.Format(docpath + @"\Report{0:yyyy_MM_dd}.xlsx",
DateTime.Now.Date);
xlWorkBook = xlApp.Workbooks.Open(path1, 0, true, 1, "", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
"\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excell.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
List<string> Data = new List<string>();
for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
{
for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
{
str = (string)(range.Cells[rCnt, cCnt] as Excell.Range).Value2;
Data.Add(str);
}
}
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
string path2 = saveFileDialog1.FileName;
saveFileDialog1.Filter = "Excel Workbook(*.xlsx)|*.xlsx";
Excell.Application App;
Excell.Workbook WorkBook;
Excell.Worksheet WorkSheet;
App = new Excell.ApplicationClass();
WorkBook = App.Workbooks.Add(BlankValue);
WorkSheet = (Excell.Worksheet)WorkBook.Worksheets.get_Item(1);
//Inserting Data In Sheet1 With Column Headings.
int count = 0;
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
StreamWriter wText = new StreamWriter(myStream);
for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
{
for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
{
WorkSheet.Cells[rCnt, cCnt] = Data[count];
count++;
}
}
//File.Copy(path1, saveFileDialog1.FileName);
WorkBook.SaveAs(myStream.ToString()[0], Excell.XlFileFormat
.xlWorkbookDefault, BlankValue, BlankValue, false, BlankValue,
Excell.XlSaveAsAccessMode.xlNoChange, BlankValue, BlankValue,
BlankValue, BlankValue, BlankValue);
WorkBook.Close(true, null, null);
//wText.Close();
//myStream.Close();
myStream.Dispose();
wText.Dispose();
App.Quit();
}
}
}
推荐答案
您在应用程序中引用的是哪个版本的MS Excel组件?,如果其MS Excel 2003使用扩展名.xls而不是.xlsx.
which version of MS Excel component you are referring in you app?, if its MS Excel 2003, use the extension .xls instead of .xlsx.
这篇关于Excel文件扩展名问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文