问题描述
如何在C#..。net framework 3.0中将.xls文件转换为.xlsx
我的代码是这样的:
how to convert a .xls file to .xlsx in C#.. .Net framework 3.0
my code is like this::
if (extension == ".xls")
{
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
flpd.SaveAs(filename1);
stream = File.Open(filename1, FileMode.Open, FileAccess.Read);
excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else if (extension == ".xlsx")
{
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
flpd.SaveAs(filename1);
stream = File.Open(filename1, FileMode.Open, FileAccess.Read);
excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
我的实际问题是,当我在数据库或服务器上的物理位置上传.xls文件时,我的日期值列被改为其他格式,也许是由于这行excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
但是.xlsx工作正常,所以我想转换在上传d文件之前.xls到.xlsx ...
有没有更好的方法...因为我需要为.xls提供选项以及用户
My actual problem is that when i am uploading .xls file in database or on physical location on server ,, my column with date values is getting changed into some other format,,ie maybe due to this line excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
but with .xlsx its working fine, so iwas thinking of converting .xls to .xlsx before uploading d file...
Is there any better way... because i need to give option for .xls as well to d user
推荐答案
private void Excel03to07(string fileName)
{
string svfileName = Path.ChangeExtension(fileName, ".xlsx");
object oMissing = Type.Missing;
var app = new Microsoft.Office.Interop.Excel.Application();
var wb = app.Workbooks.Open(fileName, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
wb.SaveAs(svfileName, XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
app.Quit();
}
这篇关于c#中的.xls到.xlsx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!