回复 2楼roger.wang的帖子
private void TSMenuItem_Open_Click(object sender, EventArgs e)
{
open_dlg.Filter = "Excel文件 (*.xlsx)|*.xlsx";
open_dlg.FilterIndex = 0;
open_dlg.RestoreDirectory = true;
open_dlg.Title = "请选择要导入的Excel文件";
bool a = false;
if (open_dlg.ShowDialog() == DialogResult.OK)
{
fpSpread1.Sheets.Clear();
fpSpread1.Visible = true;
string fileName = open_dlg.FileName;
System.Data.DataTable _Table = GetExcelTableName(fileName);
for (int j = 0; j < _Table.Rows.Count; j++)
{
if (_Table.Rows[j][2].ToString() == "Sheet1$")
{
a = true;
}
}
if (a == false)
{
MessageBox.Show("请检查导入文件格式和sheet名称!!");
}
for (int i = 0; i < _Table.Rows.Count; i++)
{
string sheetname = _Table.Rows[2].ToString();
if (sheetname == "Sheet1$")//过滤掉包含“_xlnm&”的sheet
{
FarPoint.Win.Spread.SheetView newsheet = new FarPoint.Win.Spread.SheetView();
fpSpread1.Sheets.Add(newsheet);
try
{
DataSet ds = DataToDataSet(fileName, _Table.Rows[2].ToString());
dttt = DatatoDataTable(fileName);
frmWaitingBox f = new frmWaitingBox((obj, args) =>
{
Thread.Sleep(100);
newsheet.SheetName = sheetname;
}, 5, "lase Wait...", false, false);
f.ShowDialog(this);
newsheet.DataSource = ds.Tables[0];
fpSpread1.ActiveSheet.DefaultStyle.CellType = new FarPoint.Win.Spread.CellType.NumberCellType();
MessageBox.Show("数据已成功导入!");
}
catch (Exception ex)
{
System.Console.Write(ex);
}
}
}
}
}
/// <summary>保存处理好的的结果集
/// 保存处理好的的结果集
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TSMenuItem_Save_Click(object sender, EventArgs e)
{
if (fpSpread1.Sheets[0].Cells[0, 0].Text != "")
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "excel files(*.xls)|*.xls";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
// fpSpread1.ActiveSheet.Protect = false;
//fpSpread1.ActiveSheet.DataAutoCellTypes = false;
fpSpread1.ActiveSheet.DefaultStyle.CellType = new FarPoint.Win.Spread.CellType.NumberCellType();
fpSpread1.SaveExcel(saveFileDialog1.FileName, FarPoint.Win.Spread.Model.IncludeHeaders.None);
MessageBox.Show("已保存!");
}
catch (Exception ex)
{
Console.Write(ex);
}
}
}
else
{
MessageBox.Show("无任何数据可以保存!");
}
} |