Export Dữ liệu trên Listview ra Template Excel C#?

Liên hệ QC

keiOrange

Thành viên mới
Tham gia
12/10/09
Bài viết
4
Được thích
0
Mình đang làm bảng chấm công, mình hiển thị hết dữ liệu chấm công của tháng lên listivew nhưng vẫn chưa biết cách xuất nó ra một fỉle excel có sẵn tiêu đề? ai chỉ giúp mình với
Cảm ơn nhiều
 
Mình đang làm bảng chấm công, mình hiển thị hết dữ liệu chấm công của tháng lên listivew nhưng vẫn chưa biết cách xuất nó ra một fỉle excel có sẵn tiêu đề? ai chỉ giúp mình với
Cảm ơn nhiều
Bạn nói rõ hơn chút nữa đi (nếu bạn đã có dữ liệu để đưa vào LV rồi thì từ LV xuất ra file gì nữa sao không Save as dữ liệu đó ra) hay bạn đưa file lên thử ?
 
Cụ thể là mình cần in ra bảng chấm công của tháng theo từng phòng.
wbctaq9nu4r7xowsvkh.png


Mình cần lấy hết dữ liệu trên listview ra một Template Excel như form trên. Hiển thị sẽ giống như thế.
Bạn nào biết giúp mình với....
 
bạn tham khảo code này xem sao. Đây mình viết chuyển từ C# sang wold giờ bạn dựa vào code đó bạn chuyển sang excel nhé,
void Export2Word()
{
//Tạo c c đối tượng application, document, table của MS Word
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.
Document doc;
Microsoft.Office.Interop.Word.
Table table;
//Hiện (mở) ứng dụng word
app.Visible = true;
//Tham số truyền v…o c c h…m c¢ đối l… tuỳ chọn
object obj = Type.Missing;
//Tạo một t…i liệu mới (để chứa dữ liệu xuất ra)
doc = app.Documents.Add(ref obj, ref obj, ref obj, ref obj);
Microsoft.Office.Interop.Word.
Range range = doc.Range(ref obj, ref obj);
//Thˆm một bảng c¢ 17 cột v… số h…ng bằng với số h…ng trong datatable.
table = doc.Tables.Add(range, objDataTable.Rows.Count, 17, ref obj, ref obj);
//Xuất dữ liệu từ datatable sang bảng (trong word). Ch£ ý: đối với c c đối tượng tập hợp
// trong word th phần tử đầu tiˆn c¢ chỉ số l… 1 thay v 0 như trong C#
for (int i = 0; i < objDataTable.Rows.Count; i++)
{
doc.Tables[1].Rows[i + 1].Cells[1].Range.Text = objDataTable.Rows[
"IDlopdat"].ToString();
doc.Tables[1].Rows[i + 1].Cells[2].Range.Text = objDataTable.Rows[
"tenloaidat"].ToString();
doc.Tables[1].Rows[i + 1].Cells[3].Range.Text = objDataTable.Rows[
"h"].ToString();
doc.Tables[1].Rows[i + 1].Cells[4].Range.Text = objDataTable.Rows[
"C"].ToString();
doc.Tables[1].Rows[i + 1].Cells[5].Range.Text = objDataTable.Rows[
"γk"].ToString();
doc.Tables[1].Rows[i + 1].Cells[6].Range.Text = objDataTable.Rows[
"í"].ToString();
doc.Tables[1].Rows[i + 1].Cells[7].Range.Text = objDataTable.Rows[
"a"].ToString();
doc.Tables[1].Rows[i + 1].Cells[8].Range.Text = objDataTable.Rows[
"Cc"].ToString();
doc.Tables[1].Rows[i + 1].Cells[9].Range.Text = objDataTable.Rows[
"Cà"].ToString();
doc.Tables[1].Rows[i + 1].Cells[10].Range.Text = objDataTable.Rows[
"Ch"].ToString();
doc.Tables[1].Rows[i + 1].Cells[11].Range.Text = objDataTable.Rows[
"Cr"].ToString();
doc.Tables[1].Rows[i + 1].Cells[12].Range.Text = objDataTable.Rows[
"e"].ToString();
doc.Tables[1].Rows[i + 1].Cells[13].Range.Text = objDataTable.Rows[
"Es"].ToString();
doc.Tables[1].Rows[i + 1].Cells[14].Range.Text = objDataTable.Rows[
"k"].ToString();
doc.Tables[1].Rows[i + 1].Cells[15].Range.Text = objDataTable.Rows[
"ν"].ToString();
doc.Tables[1].Rows[i + 1].Cells[16].Range.Text = objDataTable.Rows[
"W"].ToString();
doc.Tables[1].Rows[i + 1].Cells[17].Range.Text = objDataTable.Rows[
"Cv"].ToString();
}
//Thˆm đường viền cho Table nếu cần.
doc.Select();
Microsoft.Office.Interop.Word.
WdLineStyle BorderValue = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
app.Selection.Borders[Microsoft.Office.Interop.Word.
WdBorderType.wdBorderTop].LineStyle = BorderValue;
app.Selection.Borders[Microsoft.Office.Interop.Word.
WdBorderType.wdBorderBottom].LineStyle = BorderValue;
app.Selection.Borders[Microsoft.Office.Interop.Word.
WdBorderType.wdBorderLeft].LineStyle = BorderValue;
app.Selection.Borders[Microsoft.Office.Interop.Word.
WdBorderType.wdBorderRight].LineStyle = BorderValue;
app.Selection.Borders[Microsoft.Office.Interop.Word.
WdBorderType.wdBorderVertical].LineStyle = BorderValue;
app.Selection.Borders[Microsoft.Office.Interop.Word.
WdBorderType.wdBorderHorizontal].LineStyle = BorderValue;
}
private void button2_Click(object sender, EventArgs e)
{
Export2Word();
}
 
Export dữ liệu

bạn ơi tớ đã thử làm xuất ra word như bạn nhưng xảy la lỗi sau:
Tớ dùng DataTable để chứa dữ liệu trên listview -> nó giống objDataTable của bạn
...
Nhưng đến đoạn này:

Microsoft.Office.Interop.Word.Range range = doc.Range(ref obj, ref obj);
-->Erro:
Cannot implicitly convert type 'Word.Range' to 'Microsoft.Office.Interop.Word.Range'. An explicit conversion exists (are you missing a cast?)
//Thˆm một bảng c¢ 17 cột v… số h…ng bằng với số h…ng trong datatable.
table = doc.Tables.Add(range, objdtbData.Rows.Count, 17, ref obj, ref obj);

-->Erro1: The best overloaded method match for 'Word.Tables.Add(Word.Range, int, int, ref object, ref object)' has some invalid arguments
-->Erro2: cannot convert from 'Microsoft.Office.Interop.Word.Range' to 'Word.Range'

Tại vì tớ chưa làm nên cũng hiểu nguyên nhân lỗi là do đâu. Mong bạn chỉ giúp
 
bạn ơi tớ đã thử làm xuất ra word như bạn nhưng xảy la lỗi sau:
Tớ dùng DataTable để chứa dữ liệu trên listview -> nó giống objDataTable của bạn
...
Nhưng đến đoạn này:

Microsoft.Office.Interop.Word.Range range = doc.Range(ref obj, ref obj);
-->Erro: Cannot implicitly convert type 'Word.Range' to 'Microsoft.Office.Interop.Word.Range'. An explicit conversion exists (are you missing a cast?)
//Thˆm một bảng c¢ 17 cột v… số h…ng bằng với số h…ng trong datatable.
table = doc.Tables.Add(range, objdtbData.Rows.Count, 17, ref obj, ref obj);

-->Erro1: The best overloaded method match for 'Word.Tables.Add(Word.Range, int, int, ref object, ref object)' has some invalid arguments
-->Erro2: cannot convert from 'Microsoft.Office.Interop.Word.Range' to 'Word.Range'

Tại vì tớ chưa làm nên cũng hiểu nguyên nhân lỗi là do đâu. Mong bạn chỉ giúp
thứ nhất để làm được nó không báo lỗi bạn phải vào Refeder của nó khai báo hai thư viện trong C# là : microsof word và Liber... trong đó thì nó mới kết nối đc với word.
trong bài của mình , mình muốn đưa tất cả các dữ liệu từ DataGridview ra văn bản word.
bạn có thể xem bài làm của mình thử xem sao nhé.
code đó mình để trong form Lớp đất
bạn xem thêm một ví dụ nữa nhé, xem cái bài của mình thì hơi dài. xem ví dụ àny dễ hiểu hơn nè
 
Lần chỉnh sửa cuối:
load cơ sở dữ liệu(sql) lên datagridview rồi export ra file với các đinh dạng bất kì

Rất mong được các bạn tham khảo. Mình đang làm bài quản lí điểm thi viết trên ngôn ngữ csharp sử dụng cơ sở dữ liệu sql 2008, khi mình export dữ liệu từ datagridview ra file excel nếu post ra excel lần thứ 2 thì dữ liệu trong file excel lần đầu tiên sẽ bị đè lên.
Mình mong nhận được ý kiến của các bạn trên diễn đàn trong thời gian sớm nhất.
Mình cảm ơn các bạn đã quan tâm.
 
Web KT
Back
Top Bottom