我在导出execl的时候是在本地放一个空的execl,然后把数据复制到第二页,
现在我数据已经在第二页,我想把第一页删除,该如何操作,或者是说根本操作不了。
在本地建一张空excel然后把数据复制上去是为了使没有装OFFICE的人也能导出execl。
求高手~!。
现在我数据已经在第二页,我想把第一页删除,该如何操作,或者是说根本操作不了。
在本地建一张空excel然后把数据复制上去是为了使没有装OFFICE的人也能导出execl。
求高手~!。
解决方案 »
- 大批量插入数据问题。。。
- 一个图像旋转的问题
- Request.Form[].Tostring();取值错误的问题----20110513
- 文本框点击事件(100分)
- 请教高手,如何在RasterFileBrower里面实现文件夹的拖放。
- 当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'Survey_Question' 中的标识列插入显式值。
- DataGrid 中增加新的行
- Microsoft Agent Control 2.0 桌面精灵
- C# WFP tabcontrol 问题
- 如何在程序中实现TabControl的换页?
- 请问List查询
- winform treeview的checkbox点击偶尔失灵
using WorkBook = Microsoft.Office.Interop.Excel.Workbook;
using WorkSheet = Microsoft.Office.Interop.Excel.Worksheet;
using ExcelNameSpace = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.Reflection;
namespace Microsoft.Office
{
public static class ExcelOperator
{
private const string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='{0}';Extended Properties='Excel 8.0;HDR={1};IMEX=1';";
private const string SelectStr = "Select * From [{0}$A:IU];";//"Select * From [{0}$];" A:IU表示最多256列
private const char WorkSheetMagicChar = '$';
private const char WorkSheetMagicChar2 = '\'';
public static DataSet GetDataSet(string xlsFileName)
{
return GetDataSet(xlsFileName,true);
}
public static DataSet GetDataSet(string xlsFileName,bool hasTitle)
{
try
{
DataSet dataset = new DataSet();
String[] names = ListAllSheetNames(xlsFileName);
if (names == null || names.Length == 0)
{
return dataset;
}
else
{
using (OleDbConnection conn = OpenConnection(xlsFileName, hasTitle))
{
foreach (string name in names)
{
String selectstr = String.Format(SelectStr, name, hasTitle ? "Yes" : "No");
OleDbDataAdapter adapter = new OleDbDataAdapter(selectstr, conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
dt.TableName = name.TrimEnd(WorkSheetMagicChar);
dataset.Tables.Add(dt);
}
conn.Close();
}
}
return dataset;
}
catch (Exception ex)
{
throw new ExcelOperatorException("读取xls文件数据发生错误。", ex);
}
}
public static DataTable GetDataTable(string xlsFileName,string sheetName)
{
return GetDataTable(xlsFileName,sheetName, true);
}
public static DataTable GetDataTable(string xlsFileName,string sheetName, bool hasTitle)
{
try
{
DataTable dt = new DataTable();
sheetName = sheetName.TrimEnd(WorkSheetMagicChar);
using (OleDbConnection conn = OpenConnection(xlsFileName, hasTitle))
{
String selectstr = String.Format(SelectStr, sheetName, hasTitle ? "Yes" : "No");
OleDbDataAdapter adapter = new OleDbDataAdapter(selectstr, conn);
dt.TableName = sheetName;
adapter.Fill(dt);
}
return dt;
}
catch (Exception ex)
{
throw new ExcelOperatorException("读取xls文件数据发生错误。", ex);
}
}
/// <summary>
/// 将指定的<see cref="DataSet"/>对象保存到指定的文件中,若文件存在,则会覆盖该文件。
/// </summary>
/// <param name="fileName">文件路径全名。</param>
/// <param name="dataSet">要保存的<see cref="DataSet"/>对象。</param>
/// <exception cref="ExcelOperatorException"></exception>
public static void SaveData(string fileName, DataSet dataSet)
{
if (dataSet == null)
{
throw new ArgumentNullException("dataSet");
}
ExcelApp excelApp = new ExcelApp();
WorkBook wBook = excelApp.Workbooks.Add(true);
excelApp.Visible = false;
for (int i = 0; i < dataSet.Tables.Count; i++)
{
DataTable dataTable = dataSet.Tables[i];
WorkSheet wSheet;
if (wBook.Worksheets.Count > i)//默认的WorkSheet比表多,则不需要创建新的WorkSheet
{
wSheet = wBook.Worksheets[i + 1] as WorkSheet;
if (!string.IsNullOrEmpty(dataTable.TableName) && dataTable.TableName != wSheet.Name)
{
wSheet.Name = dataTable.TableName.TrimEnd(WorkSheetMagicChar);
}
}
else//创建新的WorkSheet
{
wSheet = wBook.Worksheets.Add(Type.Missing, wBook.Worksheets[i], 1, Type.Missing) as WorkSheet;
if (!string.IsNullOrEmpty(dataTable.TableName) && dataTable.TableName != wSheet.Name)
{
wSheet.Name = dataTable.TableName.TrimEnd(WorkSheetMagicChar);
}
}
//添加标题
for (int j = 0; j < dataTable.Columns.Count; j++)
{
wSheet.Cells[1, j + 1] = dataTable.Columns[j].ColumnName;
}
//添加数据
for (int k = 0; k < dataTable.Rows.Count; k++)
{
for (int j = 0; j < dataTable.Columns.Count; j++)
{
wSheet.Cells[k + 2, j + 1] = dataTable.Rows[k][j].ToString();
}
}
Marshal.ReleaseComObject(wSheet);
wSheet = null;
}
excelApp.DisplayAlerts = false;
excelApp.AlertBeforeOverwriting = false;
(wBook.Worksheets[1] as WorkSheet).Activate();
wBook.SaveAs(fileName,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
ExcelNameSpace.XlSaveAsAccessMode.xlNoChange, Missing.Value,
Missing.Value, Missing.Value,Missing.Value, Missing.Value);
excelApp.Quit();
Marshal.ReleaseComObject(wBook);
Marshal.ReleaseComObject(excelApp);
wBook = null;
excelApp = null; }
/// <summary>
/// 将指定的<see cref="DataTable"/>对象保存到指定的文件中,若文件存在,则会覆盖该文件。
/// </summary>
/// <param name="fileName">文件路径全名。</param>
/// <param name="dataTable">要保存的<see cref="DataTable"/>对象。</param>
/// <exception cref="ExcelOperatorException"></exception>
public static void SaveData(string fileName, DataTable dataTable)
{
if (dataTable == null)
{
throw new ArgumentNullException("dataTable");
}
DataSet ds = new DataSet();
ds.Tables.Add(dataTable);
SaveData(fileName, ds);
}
/// <summary>
/// 列出所有的工作表名称
/// </summary>
/// <returns></returns>
public static string[] ListAllSheetNames(string xlsFileName)
{
try
{
if (!File.Exists(xlsFileName))
{
throw new FileNotFoundException("要求的文件不存在。");
}
List<string> lst = new List<string>();
using (OleDbConnection conn = OpenConnection(xlsFileName ,false))
{
using (DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] { null, null, null, "TABLE" }))
{
foreach (DataRow dr in dt.Rows)
{
string str = dr["TABLE_NAME"].ToString();
str = str.TrimStart(WorkSheetMagicChar2);//sheet名称以数字打头时会用单引号括起来
str = str.TrimEnd(WorkSheetMagicChar2);
str = str.TrimEnd(WorkSheetMagicChar);
if (!str.Contains(WorkSheetMagicChar.ToString()))
{
lst.Add(str);
}
}
}
conn.Close();
}
return lst.ToArray();
}
catch (Exception ex)
{
throw new ExcelOperatorException("列取工作表发生错误。", ex);
}
} private static OleDbConnection OpenConnection(string xlsFileName, bool hasTitle)
{
OleDbConnection conn = new OleDbConnection(string.Format(ConnStr, xlsFileName, hasTitle ? "Yes" : "No"));
conn.Open();
return conn;
}
}
}
http://sourceforge.net/projects/myxls/