111,120
社区成员
发帖
与我相关
我的任务
分享
首先有个 存有数据的DATASET ds
if (ds.Tables.Count == 0)
return;
//实例化一个Excel.Application对象
Excel.Application excel = new Excel.Application();
//新增加一个工作簿,Workbook是直接保存,不会弹出保存对话框,加上Application会弹出保存对话框,值为false会报错
excel.Application.Workbooks.Add(true);
//让后台执行设置为不可见,为true的话会看到打开一个Excel,然后数据在往里写
excel.Visible = true;
//生成Excel中列头名称
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
excel.Cells[1, i + 1] = ds.Tables[0].Columns[i].ColumnName;//输出DataGridView列头名
}
//把DataGridView当前页的数据保存在Excel中
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)//控制Excel中行,上下的距离,就是可以到Excel最下的行数,比数据长了报错,比数据短了会显示不完
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)//控制Excel中列,左右的距离,就是可以到Excel最右的列数,比数据长了报错,比数据短了会显示不完
{
string str = ds.Tables[0].Rows[i][j].ToString();
excel.Cells[i + 2, j + 1] = "'" + str;//i控制行,从Excel中第2行开始输出第一行数据,j控制列,从Excel中第1列输出第1列数据,"'" +是以string形式保存,所以遇到数字不会转成16进制
}
}
}
//设置禁止弹出保存和覆盖的询问提示框
excel.DisplayAlerts = false;
excel.AlertBeforeOverwriting = false;
//保存工作簿,值为false会报错
excel.Application.Workbooks.Add(true).Save();
//保存excel文件
excel.Save("你保存的文件名.xls");
//确保Excel进程关闭
excel.Quit();
excel = null;
//将dataset导出Excel
public static Boolean ExportForDataGridview(DataGridView gridView, string fileName, bool isShowExcle)
{
Excel.Application app = new Excel.Application();//上面语句原版,本环境中不对,改成本句可以了
try
{
if (app == null)
{
return false;
}
app.Visible = isShowExcle;
Workbooks workbooks = app.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
if (worksheet == null)
{
return false;
}
string sLen = "";
//取得最后一列列名
char H = (char)(64 + gridView.ColumnCount / 26);
char L = (char)(64 + gridView.ColumnCount % 26);
if (gridView.ColumnCount < 26)
{
sLen = L.ToString();
}
else
{
sLen = H.ToString() + L.ToString();
}
//标题
string sTmp = sLen + "1";
Range ranCaption = worksheet.get_Range(sTmp, "A1");
string[] asCaption = new string[gridView.ColumnCount];
for (int i = 0; i < gridView.ColumnCount; i++)
{
asCaption[i] = gridView.Columns[i].HeaderText;
}
ranCaption.Value2 = asCaption;
//数据
object[] obj = new object[gridView.Columns.Count];
for (int r = 0; r < gridView.RowCount; r++)
{
for (int l = 0; l < gridView.Columns.Count; l++)
{
if (gridView[l, r].ValueType == typeof(DateTime))
{
obj[l] = "'" + gridView[l, r].Value.ToString();
}
else
{
obj[l] ="'" + gridView[l, r].Value;
}
}
string cell1 = sLen + ((int)(r + 2)).ToString();
string cell2 = "A" + ((int)(r + 2)).ToString();
Range ran = worksheet.get_Range(cell1, cell2);
ran.Value2 = obj;
}
//保存
workbook.SaveCopyAs(fileName);
workbook.Saved = true;
}
finally
{
//关闭
app.UserControl = false;
app.Quit();
}
return true;
}
//将Excel表格导入DataSet
public static DataSet ExcelToDataSet(string filename, string tbname)
{
OleDbConnection cnnxls = new OleDbConnection();
DataSet myDs = new DataSet();
try
{
string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ;Extended Properties=Excel 8.0;Data Source =" + filename;
cnnxls.ConnectionString = mystring;
cnnxls.Open();
OleDbDataAdapter myDa = new OleDbDataAdapter("SELECT * from [" + tbname.Trim() + "]", cnnxls);
myDa.Fill(myDs);
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message, "错误");
}
finally
{
cnnxls.Close();
}
return myDs;
}
//
public static string[] GetExcelTableNames(string filename)
{
System.Data.DataTable dt = new System.Data.DataTable();
OleDbConnection cnnxls = new OleDbConnection();
try
{
string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ;Extended Properties=Excel 8.0;Data Source =" + filename;
cnnxls.ConnectionString = mystring;
cnnxls.Open();
dt = cnnxls.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
}
finally
{
cnnxls.Close();
}
List<string> names = new List<string>();
foreach (DataRow row in dt.Rows)
{
names.Add(row["TABLE_NAME"].ToString());//.Trim('\'', '$').Replace("''", "'").Replace("$$", "$"));
}
return names.ToArray();
}
}