c#这样创建excel文件对吗?代码如下,为什么打开后只有一个表,而且不能添加新表 10
StringrootPath=Server.MapPath("~");StringBuilderMyStringBuilder=newStringBuilder(root...
String rootPath = Server.MapPath("~");
StringBuilder MyStringBuilder = new StringBuilder(rootPath);
MyStringBuilder.Append("\\Excel_Files\\");
DateTime dtNow = new DateTime();
dtNow = DateTime.Now;
DateTime dtYesterday = dtNow.AddDays(-1);
string stryear = dtYesterday.Year.ToString();
string strmonth = dtYesterday.Month.ToString().PadLeft(2, '0');
string strday = dtYesterday.Day.ToString().PadLeft(2,'0');
MyStringBuilder.Append(stryear);
MyStringBuilder.Append(strmonth);
string ExcelFil = MyStringBuilder.ToString();
System.IO.Directory.CreateDirectory(ExcelFil);
// MyStringBuilder.Append(strday);
MyStringBuilder.Append("\\");
MyStringBuilder.Append(stryear);
MyStringBuilder.Append(strmonth);
MyStringBuilder.Append(strday);
MyStringBuilder.Append(".xls");
string ExcelFile = MyStringBuilder.ToString();
if (!System.IO.Directory.Exists(ExcelFile))
{
Console.WriteLine("检查当前文件夹不存在,正在创建.....");
FileStream Newtext = File.Create(ExcelFile);
Newtext.Close();
Console.WriteLine("创建成功!");
} 展开
StringBuilder MyStringBuilder = new StringBuilder(rootPath);
MyStringBuilder.Append("\\Excel_Files\\");
DateTime dtNow = new DateTime();
dtNow = DateTime.Now;
DateTime dtYesterday = dtNow.AddDays(-1);
string stryear = dtYesterday.Year.ToString();
string strmonth = dtYesterday.Month.ToString().PadLeft(2, '0');
string strday = dtYesterday.Day.ToString().PadLeft(2,'0');
MyStringBuilder.Append(stryear);
MyStringBuilder.Append(strmonth);
string ExcelFil = MyStringBuilder.ToString();
System.IO.Directory.CreateDirectory(ExcelFil);
// MyStringBuilder.Append(strday);
MyStringBuilder.Append("\\");
MyStringBuilder.Append(stryear);
MyStringBuilder.Append(strmonth);
MyStringBuilder.Append(strday);
MyStringBuilder.Append(".xls");
string ExcelFile = MyStringBuilder.ToString();
if (!System.IO.Directory.Exists(ExcelFile))
{
Console.WriteLine("检查当前文件夹不存在,正在创建.....");
FileStream Newtext = File.Create(ExcelFile);
Newtext.Close();
Console.WriteLine("创建成功!");
} 展开
1个回答
展开全部
你的这种方法我第一次看到,我看到最多的和最近做的程序里面都是通过在程序引用里引用COM中的excel对象程序库,或者外部的excel9.olb,然后在程序中用using/.imports 添Microsoft.Office.Interop。。。以下是VB.NET的写法 Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
xlApp = CreateObject("Excel.Application")
xlBook = xlApp.Workbooks.Open(Application.StartupPath & "\template\Line.xls")
xlSheet = xlBook.Sheets("templete")
你参考一下。。因为这个是专为EXCEL连接设置的对象库。。所以对EXCEL的支持很广泛
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
xlApp = CreateObject("Excel.Application")
xlBook = xlApp.Workbooks.Open(Application.StartupPath & "\template\Line.xls")
xlSheet = xlBook.Sheets("templete")
你参考一下。。因为这个是专为EXCEL连接设置的对象库。。所以对EXCEL的支持很广泛
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询