1
Q
導出數據表
A
回答
0
的XAML是:
<Window x:Class="ReadExcel_And_BindToDataGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="490" Width="900" WindowStartupLocation="CenterScreen" WindowStyle="None" ResizeMode="NoResize">
<Grid>
<Border BorderBrush="Black" BorderThickness="5">
<StackPanel HorizontalAlignment="Center">
<Border BorderBrush="Black" BorderThickness="0,0,0,5" Height="60" HorizontalAlignment="Center" VerticalAlignment="Center" Width="890">
<WrapPanel HorizontalAlignment="Left" VerticalAlignment="Center">
<TextBox Height="30" Name="txtFilePath" Width="550" Margin="10,0,10,0" FontSize="15" Opacity="1" />
<Button Content="Browse Excel" Name="btnOpen" Width="120" FontSize="15" Height="30" FontWeight="Bold" Click="btnOpen_Click" />
<Button Content="Close" FontSize="15" Height="30" FontWeight="Bold" Name="btnClose" Width="75" Margin="110,0,0,0" Background="#FFEFE5E5" Click="btnClose_Click" />
</WrapPanel>
</Border>
<Label Name="txtIns" Content="View Uploaded records to DataGridView" FontSize="15" FontWeight="Bold" Margin="15" HorizontalAlignment="Center" />
<DataGrid AutoGenerateColumns="True" IsReadOnly="True" HorizontalAlignment="Center" Name="dtGrid" VerticalAlignment="Center" />
</StackPanel>
</Border>
</Grid>
</Window>
後面的代碼是:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnOpen_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.DefaultExt = ".xlsx";
openfile.Filter = "(.xlsx)|*.xlsx";
//openfile.ShowDialog();
var browsefile = openfile.ShowDialog();
if (browsefile == true)
{
txtFilePath.Text = openfile.FileName;
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
//Static File From Base Path...........
//Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open(AppDomain.CurrentDomain.BaseDirectory + "TestExcel.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
//Dynamic File Using Uploader...........
Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open(txtFilePath.Text.ToString(), 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Microsoft.Office.Interop.Excel.Worksheet excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelBook.Worksheets.get_Item(1); ;
Microsoft.Office.Interop.Excel.Range excelRange = excelSheet.UsedRange;
string strCellData = "";
double douCellData;
int rowCnt = 0;
int colCnt = 0;
DataTable dt = new DataTable();
for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
{
string strColumn = "";
strColumn = (string)(excelRange.Cells[1, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
dt.Columns.Add(strColumn, typeof(string));
}
for (rowCnt = 2; rowCnt <= excelRange.Rows.Count; rowCnt++)
{
string strData = "";
for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++)
{
try
{
strCellData = (string)(excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
strData += strCellData + "|";
}
catch (Exception ex)
{
douCellData = (excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2;
strData += douCellData.ToString() + "|";
}
}
strData = strData.Remove(strData.Length - 1, 1);
dt.Rows.Add(strData.Split('|'));
}
dtGrid.ItemsSource = dt.DefaultView;
excelBook.Close(true, null, null);
excelApp.Quit();
}
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
相關問題
- 1. 導出數據表到Excel
- 2. 從oracle導出表數據
- 3. 無法導出數據表
- 4. 從表中導出數據
- 5. 導出數據和固定數據表
- 6. 導出數據表中的數據
- 7. 的數據導出到數據表
- 8. 導出數據
- 9. 導出數據
- 10. 導出數據
- 11. 導出數據
- 12. 導出數據
- 13. JMeter報告儀表板錯誤 - 數據導出器「html」無法導出數據
- 14. 帶導出選項的數據表應該只導出選定的數據
- 15. 導入和導出數據
- 16. 導入/導出數據庫
- 17. RavenDB導出/導入數據
- 18. 蒲公英數據表導出到Excel - 缺少導出鏈接
- 19. 將數據庫表導出到模型導出
- 20. 角度數據表導出錯誤
- 21. 從MYSQL表中導出特定數據
- 22. 數據表 - CSV僅導出當前行
- 23. 導出HTML數據表到Excel
- 24. Ms-Access表單數據導出到Word
- 25. 隱藏div導出數據表
- 26. 從多個數據表導出到Excel
- 27. 導出到數據庫表錯誤
- 28. Oracle數據庫導出缺少表
- 29. 將HTML表格數據導出到Excel
- 30. 將數據導出到excel表格