DataTable DTDaoRu = new DataTable(); OpenFileDialog opf = new OpenFileDialog(); opf.Filter = "电子表格|*.xls;*.xlsx"; if (opf.ShowDialog() == DialogResult.OK) { Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(opf.FileName); Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells; Aspose.Cells.ExportTableOptions p = new Aspose.Cells.ExportTableOptions(); p.ExportAsString = true;//内容转化为字符串 p.ExportColumnName = true;//首行转换列名 DataTable dt = cells.ExportDataTable(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, p); }
循环导出
for (int i = 2; i < cells.MaxDataRow + 1; i++)//前两行是标题和列名
{
List<object> clist = new List<object>();
for (int j = 0; j < cells.MaxDataColumn + 1; j++)
{
string s = cells[i, j].StringValue.Trim();
clist.Add(s);
}
GridRow gr = new GridRow(clist);
spgData.SPG.PrimaryGrid.Rows.Add(gr);
}
导出
It_helped ISs = new It_helped(); if (mydgv.Rows.Count > 0) { List<int> Basetype = new List<int>(); DataTable DTS = new DataTable(); for (int a = 0; a < mydgv.Columns.Count; a++) { if (mydgv.Columns[a].Visible == false) { Basetype.Add(a); } DTS.Columns.Add(mydgv.Columns[a].HeaderText); } for (int a = 0; a < mydgv.Rows.Count; a++) { if ((mydgv.Rows[a].Cells["Base_Code"].Value + "").Trim() != "") { DataRow DR = DTS.NewRow(); for (int b = 0; b < mydgv.Columns.Count; b++) { DR[b] = (mydgv.Rows[a].Cells[b].Value + "").Trim().ToString(); } DTS.Rows.Add(DR); } } for (int cc = 0; cc < Basetype.Count; cc++) { DTS.Rows.RemoveAt(int.Parse(Basetype[cc].ToString())); } string fileDefaultName = "导出Excel"; string path; SaveFileDialog savefile = new SaveFileDialog(); savefile.FileName = fileDefaultName; savefile.Filter = "EXECL文件(*.xlsx) |*.xlsx |EXECL2003文件(*.xls) |*.xls |所有文件(*.*) |*.*"; if (savefile.ShowDialog() == DialogResult.OK) { try { ISs.importExcel(DTS, US_GV.UserDeptName + "出库卫材详情", savefile.FileName); MessageBox.Show("导出成功"); } catch (Exception ex) { MessageBox.Show("导出失败:" + ex.Message); } } } else { MessageBox.Show("请检查表格是否有数据!"); return; }